![]() |
#2
lyj232011-05-22 17:22
|

#include<exception>
#include<iostream>
#include<cstdio>
using namespace std;
//Exception classes
class A{};
class B{};
//terminate() handler
void my_thandler(){
cout<<"terminate called"<<endl;
exit(0);}
//unexpected() handlers
void my_unhandler1(){throw A();}
void my_unhandler2(){throw;}
//-------------------------------------------------------------------------
void t(){throw B();}
void f()throw(A){t();}
void g()throw(A,bad_exception){t();}
//main()
int main()
{
set_terminate(my_thandler);
set_unexpected(my_uhandler1);//这里报错
try{
f();
}catch(A&){
cout<<"caught an A from f"<<endl;}
set_unexpected(my_uhandler2);//这里报错
try{
g();
}catch(bad_exception&){
cout<<"caught a bad_exception from g"<<endl;}
try{
f();
}catch(...){
cout<<"This will never print"<<endl;
}
}//:~
这个程序的执行流程到底是什么?my_uhandler1这个到底什么时候被用啊!#include<iostream>
#include<cstdio>
using namespace std;
//Exception classes
class A{};
class B{};
//terminate() handler
void my_thandler(){
cout<<"terminate called"<<endl;
exit(0);}
//unexpected() handlers
void my_unhandler1(){throw A();}
void my_unhandler2(){throw;}
//-------------------------------------------------------------------------
void t(){throw B();}
void f()throw(A){t();}
void g()throw(A,bad_exception){t();}
//main()
int main()
{
set_terminate(my_thandler);
set_unexpected(my_uhandler1);//这里报错
try{
f();
}catch(A&){
cout<<"caught an A from f"<<endl;}
set_unexpected(my_uhandler2);//这里报错
try{
g();
}catch(bad_exception&){
cout<<"caught a bad_exception from g"<<endl;}
try{
f();
}catch(...){
cout<<"This will never print"<<endl;
}
}//:~
书上实在写的太少了,高手讲解一下
[ 本帖最后由 lyj23 于 2011-5-22 07:15 编辑 ]