c++ - 异常处理 - set_unexpected() 无法调用

标签 c++

#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/

相关文章:

c++ - 设计问题 : Message processing components. .. 调用 incorect 重载函数

c++ - 我可以编写 OpenGL 应用程序而不将其绑定(bind)到某个窗口库吗?

c++ - 根据字符串列表访问 C++ 类

C++ "virtual"关键字放置

c++ - 在 C++ 中为两个参数使用 const 覆盖运算符

c++ - 关于 C++ 非 POD union 的问题

c++ - 在 cuda Thrust 中融合两个归约运算

当参数为真时 C++ 断言失败

c++ - 将 C 函数转换为 Lua 函数

c++ - 是否不再包含在 MinGW 和/或 MSYS 中?