异常处理是一个微妙的问题,你应该尽可能优雅地处理所有异常。要达到这个目的,你需要学习terminate()函数。
terminate()函数在程序抛出一个异常并且异常没有被捕获的时候被调用,像下面这样:
#include
#include
void on_terminate()
{
std::cout << "terminate() 函数被调用了!" << std::endl;
std::cin.get();
}
int main()
{
// 如果用 VC6,去掉“std::”前缀
std::set_terminate( on_terminate);
throw std::exception();
std::cout << "terminate() 函数没有被调用!" << std::endl;
std::cin.get();
return 0;
}
避免这种情形的方案一开始看起来很简单:
int main()
{
try
{
/* code */
}
catch( std::exception & exc)
{
// 记录到日志,或作其他处理
}
catch(...)
{
// 记录下“Unknown exception”
}
return 0;
}
不过,在多线程应用程序中情况变得有点复杂,因为你创建的每个线程都要有上面的(catch)处理过程。
然而terminate()函数在许多其它情况下会被调用,包括:
当你抛出一个异常,并且在它的拷贝构造函数中,另一个异常被抛出。
在堆栈展开的过程中抛出一个异常,此时析构函数抛出一个异常。
当一个静态对象的构造函数或析构函数抛出异常时。
当一个用atexit注册过的函数抛出一个异常时。
当你在代码中写下“throw;”(这意味着重新抛出当前异常),然而并没有当前异常时。
当一个函数抛出一个它的异常说明不允许的异常时
当默认的unexpected()处理过程被调用时
下面的代码演示了上面各种情况下的结果:
#include
#include
void on_terminate()
{ std::cout << "terminate()函数被调用了!" << std::endl;
std::cin.get(); }
[1]
struct custom_exception
{
custom_exception() {}
custom_exception( const custom_exception &)
{ throw std::exception(); }
};
void case_1()
{
try
{ throw custom_exception(); }
catch(...)
{}
}
[2]
struct throw_in_destructor
{
~throw_in_destructor() { throw std::exception(); }
};
void case_2()
{
try
{
throw_in_destructor temp;
throw std::exception();
}
catch(...)
{}
}
来源:考试大-Java认证
责编:冷客 评论 纠错
上一页1