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

Exception Handling - set_unexpected() not able to call

本文关键字:调用 unexpected set 异常处理      更新时间:2023-10-16
#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()函数不应返回,但它可以引发(或重新引发)异常。如果它抛出以前违反的异常规范所允许的新异常,则在调用其异常规范的函数时,将继续搜索另一个处理程序。