一个异常问题~~~
#include <iostream>
#include <exception>
using namespace std;
class Up{};
class Fit{};
void g();
void f(int i)throw(Up,Fit){
switch(i)
{case 1:
throw Up();break;
case 2:
throw Fit();break;
}
g();
}
void g(){throw 47;}
void my_unexpected(){
cout<<"unexpected exception thrown"<<endl;
exit(0);
}
int main(){
set_unexpected(my_unexpected);
for(int i=1;i<=3;i++)//???运行到i=3的时候按理说应该会调用set_unexpected(my_unexpected)函数啊
try{f(i);}
catch(Up&){cout<<"Up caught"<<endl;}
catch(Fit&){cout<<"Fit caught"<<endl;}
return 0;
}
望高人赐教
[解决办法]
#include <iostream> #include <exception> using namespace std; class Up{}; class Fit{}; void g(); void my_unexpected(){ cout <<"unexpected exception thrown" <<endl; exit(0); } void f(int i)throw(Up,Fit){ switch(i) { case 1: throw Up();break; case 2: throw Fit();break; default:// 这里没有对i!=1 ,2之外的情况进行处理所会抛出异常。 throw my_unexpected();break;// 晕o!我以为什么事哦!你在加上你的处理就可以啊,我之前没看到你这个函数是用来干嘛啊! } g(); } void g(){ throw 47;} int main(){ set_unexpected(my_unexpected); for(int i=1;i <=3;i++) try { f(i); } catch(Up&) { cout <<"Up caught" <<endl; } catch(Fit&) { cout <<"Fit caught" <<endl; } return 0; }