异常行为C++ 14 vs C++ 98

gau*_*waj 6 c++ exception c++98 c++14

我写了以下程序

#include <iostream>
#include <stdexcept>

class Myclass
{
    public:
    ~Myclass() 
    {
        //throw std::runtime_error("second (in destructor)");
        throw 1;
    }
};

void fun()
{
    Myclass obj;
}
int main()
{   
    try
    {
        fun();      
    }
    catch (const std::exception& e)
    {
       std::cout << e.what();
    }
    catch(...)
    {
       std::cout << " ... default Catch" << std::endl; 
    }
    std::cout << "Normal" << std::endl;
    return 0;
}  
Run Code Online (Sandbox Code Playgroud)

当我在C++98模式(cpp.sh)上运行程序时,它会打印出来

 ... default Catch
Normal
Run Code Online (Sandbox Code Playgroud)

当我使用C++14模式运行它时,它不会打印任何内容. 为什么这种行为有变化?

我确实理解,每当发生任何异常并且任何destructor(在堆栈展开过程中)抛出任何异常时,它都会终止应用程序.但是这里只有一次异常从try块中抛出destructor.

Rei*_*ica 14

从C++ 11开始,没有明确拼写异常规范的析构函数具有与默认生成的异常规范相同的异常规范.在您的情况下,默认生成的析构函数将是noexcept(大多数默认生成的析构函数),因此您的析构函数也被考虑在内noexcept.从noexcept函数中抛出会自动调用std::terminate.

如果您希望异常可以捕获,请将析构函数声明为throw:

~Myclass() noexcept(false)
{
    //throw std::runtime_error("second (in destructor)");
    throw 1;
}
Run Code Online (Sandbox Code Playgroud)

但在你这样做之前,重新考虑一下.投掷析构函数是个坏主意.