Exception thrown in the constructor: is the destructor called?

If an exception is thrown in the constructor of the object, is the destructor called? Or is this behavior undefined? (This is why I am reluctant to say what my compiler does).

struct foo()
{
    foo(){
        throw "bar";
    }
    ~foo(){
        /*am I called*/
    }
};

foo f;
+4
source share
2 answers

The destructor will not be called because the object is foonot fully constructed until the constructor completes execution (note that this means that you drop the constructor that is delegated to another constructor, then the destructor will be called). Throwing from the constructor is not performed undefined.

+3
source

, . , . , , , , .

, undefined, . .

, , , , -, , , , , .

, ( stackoverflow, google ).

+1

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


All Articles