关于例外的问题 [英] Question about Exceptions
问题描述
我只是在视觉工作室里玩异常,并且使用上面的代码,我期望,因为我的异常规范没有提到任何bad_exception应该被抛出。但实际发生的是异常被适当的处理程序所捕获。为什么这样?我在IDE中缺少一些设置或某些东西?
I was just playing around with exceptions in the visual studio and with the above code I was expecting that since my exception specification doesn't mention anything the bad_exception should have been thrown. But what actually happens is the exception gets caught by the appropriate handler. Why so? Am i missing some setting or something in the IDE?
虽然我被困在上面提到的,实际上我试图找到问题的答案,如果我有一个例外空白规范然后什么叫?
将会抛出 意外() 方法或 * bad_exception * ,如果两者都按照什么顺序?
这是代码。
While i got stuck at above mentioned, Actually I was trying to find answer to the question,If i have a exception blank specification then what gets called? the unexpected() method or a *bad_exception* will be thrown and if both in what order? Here's the code.
#include "stdafx.h"
#include <stdio.h>
#include <exception>
#include <iostream>
using namespace std;
class A
{
public:
int i;
};
void myunexpected ()
{
cerr << "unexpected called\n";
}
void doSomething(void) throw();
void doSomething(void) throw()
{
A obj;
obj.i= 100;
throw obj;
}
int _tmain(int argc, _TCHAR* argv[])
{
set_unexpected (myunexpected);
try
{
doSomething();
}
catch (bad_exception be)
{
puts("Caught something");
}
catch (A &obj)
{
puts("Caught Integer");
}
return 0;
}
推荐答案
关于异常规范,Visual Studio不符合标准。
Regarding exception specification, Visual Studio is not standard-conforming.
虽然空的异常规范有些有用(但是如上所述,VS没有正确实现),一般来说,异常规范被视为失败的实验。
While the empty exception specification is somewhat useful (but, as said, not properly implemented by VS), in general exception specifications are seen as an experiment that failed.
这篇关于关于例外的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!