异常处理 - set_unexpected() 无法调用
Exception Handling - set_unexpected() not able to call
#include "iostream"
#include "conio.h"
#include "exception"
#include "cstdlib"
using namespace std;
void myunexpected ()
{
cerr << "unexpected calledn";
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 intn"; }
catch (...) { cerr << "caught other exception (non-compliant compiler?)n"; }
getch();
return 0;
}
输出(在 Visual Studio 2008 上执行时): 捕获了其他异常(不兼容的编译器?
但是,我期望输出是:
意外调用
被抓到的
注意:我在Visual Studio 2008上执行了这个程序。
是的
,根据标准,输出应该是[#1]:
意外调用
被抓到的
海湾合作委员会给出准确的结果。
请注意,MSVC 是出了名的错误 w.r.t 处理异常规范。异常规范被视为失败的实验。
AFAIK,MSVC不实现异常规范,除了空的(throw()
/nothrow
)
C++03 标准:
[#1] 15.5.2 unexpected()
函数 [意外除外]
unexpected()
函数不应返回,但它可以引发(或重新引发)异常。如果它抛出以前违反的异常规范所允许的新异常,则在调用其异常规范的函数时,将继续搜索另一个处理程序。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 为什么当我在使用空 throw() 说明符指定的函数中抛出异常时没有调用 std::unexpected()
- 为什么不调用函数unexpected
- 为什么即使unexpected handler()抛出指定的异常,仍然调用terminate() ?
- 谁能告诉我什么时候调用terminate(),什么时候调用unexpected() ?