当函数抛出异常时,标准行为不在有效异常列表中时是什么标准行为?例如,当我运行此代码:C++异常规范 - 处理无效异常
#include <iostream>
#include <cstdlib>
using namespace std;
void NotLegal() throw(char) {
throw 42;
}
void myunexpected() {
cout << "exception not in list\n";
cin.get();
exit(0);
}
int main()
{
set_unexpected(myunexpected);
try {
NotLegal();
}
catch(...) {
cout << "exception catched";
cin.get();
}
return 0;
}
它的行为在不同的GCC和Visual Studio C++编译器:
- 2010年VS预计不会异常一般异常处理程序捕获。
- 在GCC
unexpected()
处理函数被调用而不是捕获异常。
这是什么区别?为什么MS C++编译器不会调用unexpected()
回调?
您是否尝试更改您的示例?只是为了确保你不会在42和char之间自动转换。只需尝试throw()而不是throw(char)。 –
这与空例外列表相同。 –