1

当函数抛出异常时,标准行为不在有效异常列表中时是什么标准行为?例如,当我运行此代码: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()回调?

+1

您是否尝试更改您的示例?只是为了确保你不会在42和char之间自动转换。只需尝试throw()而不是throw(char)。 –

+0

这与空例外列表相同。 –

回答

3

The documentationset_unexpected召唤出这种行为:

备注

...

C++标准要求,当一个函数抛出unexpected被称为不在其投掷列表中的例外。目前的实施不支持这一点。

1

你编译了正确的标志吗?对于某些优化版本构建,Visual Studio默认没有启用异常处理。

你想/c and /EHsc

+3

链接到的文档说* C++标准要求当函数抛出一个不在其抛出列表中的异常时调用意外。 **目前的实现不支持这个**。* –

+0

我怀疑VS C++没有实现。你能把这个写成单独的答案吗? –