#include <iostream>
using namespace std;
class Exception
{
public:
Exception()
{
cout<<"执行构造函数..."<<endl;
}
~Exception()
{
cout<<"执行析构函数..."<<endl;
}
void Display()
{
cout<<"Exception类异常!";
}
};
void ExcpFun()
{
cout<<"在Excpfun函数中触发Exception类异常!"<<endl;
cout<<"========================3========================="<<endl;
throw Exception();
}
void main()
{
cout<<"进入main函数"<<endl;
Exception Excp;
try
{
cout<<"在try模块中,调用ExcpFun函数"<<endl;
ExcpFun();
}
catch(Exception E)
{
cout<<"=========================1========================"<<endl;
cout<<"在catch块中捕获到Exception类异常:"<<endl;
E.Display();
cout<<endl;
cout<<"=========================2========================"<<endl;
}
catch(char* str)
{
cout<<"捕获到其他类型异常:"<<str<<endl;
}
cout<<"异常已被处理!"<<endl;
}
运行结果:
进入main函数
执行构造函数...
在try模块中,调用ExcpFun函数
在Excpfun函数中触发Exception类异常!
========================3=========================
执行构造函数... //此处为何有构造,而又马上析构?
执行析构函数...
=========================1========================
在catch块中捕获到Exception类异常:
Exception类异常!
=========================2========================
执行析构函数... //此处为何有两次析构?
执行析构函数...
异常已被处理!
执行析构函数...