捕获指定的类型
这样的话可以对每种异常做出不同的处理,例如:
#include
using namespace std;
void A(int n){
int a = 1;
float b = 0.2;
double c = 0.3;
if(n == 1)
throw a;
else if(n == 2)
throw b;
else if(n == 3)
throw c;
}
int main(){
int test;
while(cin >> test){
try{
A(test);
}catch(int i){
cout << "Catch int exception " << i << endl;
}catch(float i){
cout << "Catch float exception " << i << endl;
}catch(double i){
cout << "Catch double exception " << i << endl;
}
}
return 0;
}
捕获泛型
如果想捕获全部类型异常的话,C++ 提供了一种