我正在尝试处理意外的异常,但无法正常工作。这是来自:C++ Reference的示例
// set_unexpected example
#include <iostream>
#include <exception>
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"; }
return 0;
} 他们说输出应该是:输出:
意外调用
捕获整型
当我尝试的时候不会发生这种情况。我的输出是:
捕获其他异常(不符合编译器?)
我正在使用VS2010 sp1
发布于 2011-02-10 17:41:26
unexpected的Documentation说: C++标准要求当一个函数抛出一个不在其抛出列表中的异常时,调用exception。当前的实现不支持这一点。
所以答案是VC10是一个不兼容的编译器。
发布于 2011-02-10 17:46:05
Visual Studio未正确实现该标准。参见this MSDN page。将解析int,但不使用该used。
发布于 2011-02-10 17:48:15
当函数具有异常说明符时,Visual C++始终发出Warning C4290。在MSDN的同一篇文章中:“一个函数是使用异常规范声明的,Visual C++接受它,但没有实现它。”所以,这个编译器不符合标准。
https://stackoverflow.com/questions/4955249
复制相似问题