- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
#include "iostream"
#include "conio.h"
#include "exception"
#include "cstdlib"
using namespace std;
void myunexpected ()
{
cerr << "unexpected called\n";
throw 0; // throws int (in exception-specification)
}
void myfunction () throw (int)
{
throw 'x'; // throws char (not in exception-specification)
}
int main (void)
{
set_unexpected (myunexpected);
try
{
myfunction();
}
catch (int) { cerr << "caught int\n"; }
catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
getch();
return 0;
}
Output(When executed on Visual studio 2008): caught other exception (non-compliant compiler?)
But, I was expecting the output to be:
unexpected called
caught int
NOTE: I executed this program on Visual Studio 2008.
最佳答案
是的,根据标准,输出应该是[#1]:
unexpected called
caught int
gcc 给出 accurate result 。
请注意,MSVC 在处理异常规范方面是出了名的错误。异常规范被认为是 failed experiment .
AFAIK,MSVC 没有实现异常规范,空的除外 ( throw()
/nothrow
)
C++03 标准:
[#1] 15.5.2 unexpected()
函数 [except.unexpected]
The
unexpected()
function shall not return, but it can throw (or re-throw) an exception. If it throws a new exception which is allowed by the exception specification which previously was violated, then the search for another handler will continue at the call of the function whose exception specification was violated....
关于c++ - 异常处理 - set_unexpected() 无法调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10056909/
#include "iostream" #include "conio.h" #include "exception" #include "cstdlib" using namespace std;
我用的是VC2010,写了下面的代码来测试“set_unexpected”函数。 #include #include void my_unexpected_handler() { std:
我是一名优秀的程序员,十分优秀!