首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个错误有关问题~

2012-02-12 
一个异常问题~~~#include iostream#include exceptionusing namespace stdclass Up{}class Fit{}vo

一个异常问题~~~
#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;
}

望高人赐教

[解决办法]

C/C++ code
#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; } 

热点排行