Exception C ++ 14 vs C ++ 98

I wrote the following program

#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;
}  

When I run the program above in mode C++98(cpp.sh), it prints

 ... default Catch
Normal

When I run it in mode C++14, it doesn't print anything. Why is this behavior changing?

I understand that whenever an exception occurs, and any destructor(in the process of expanding the stack) throws any exception, it terminates the application. But here, only a single exception is excluded from the block trythat is from destructor.

+6
source share
2 answers

++ 11, , . , , noexcept ( , ), noexcept. noexcept std::terminate.

, , :

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

, . , .

+14

, , (, , ), std::terminate.

, ++ 11 noexcept(true) ( , ), , , std::terminate, noexcept(true).

ยง12.4/3

[class.dtor] [. , noexcept, , โ€‹โ€‹ (15.4). - ]

ยง15.4/14

[except.spec] noexcept, , .

, .

+4

Source: https://habr.com/ru/post/1016603/


All Articles