terminate()函数在程序抛出一个异常并且异常没有被捕获的时候被调用,像下面这样:
#include <exception>
#include <iostream>
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;
}