#include <iostream>
using namespace std;
#include <exception>
void dis()
{
cout<<"terminate disabled "<< endl;
}
void display() throw(int,double)
{
if(0)
throw int();
if(0)
throw double();
if(1)
throw string();
}
int main()
{
set_unexpected(dis);
try
{
display();
}
catch(int)
{
cout<<"int "<< endl;
}
catch(double)
{
cout<<"double "<< endl;
}
catch(string)
{
cout<<"string "<< endl;
}
system("pause");
return 0;
} 现在输出是
terminate disabled然后程序就终止了
但是我写的时候不是set_unexpected,而是
set_terminate(dis);输出结果是
terminate disabled
terminate disabled 为什么会有这种矛盾呢?
发布于 2011-06-21 03:08:37
因此,不完全清楚您的原始输出是什么。我尽我最大的努力把它清理干净,但是你的引用标签使它变得不明显。
在您的代码中,如果您使用set_unexpected(dis),您应该看到:
terminate disabled在你的代码中,如果你使用set_terminate(dis),你应该看到:
terminate disabled在您的代码中,如果同时使用set_unexpected(dis)和set_terminate(dis),您应该会看到:
terminate disabled
terminate disabled解决这个问题的一种方法是将dis throw 0作为最后一行。这将允许您将异常转换为您的函数声明将抛出的异常。
https://stackoverflow.com/questions/6416038
复制相似问题