What destructors are executed when the constructor throws an exception?

In C ++, if a constructor throws an exception, which destructors are triggered?

In particular, does any value matter if the exception is during the initialization list or body?

Also, what about inheritance and members? Presumably, all completed structures are destroyed. If only some members are created, delete only those who destroy them? If multiple inheritance exists, do all the destroyed constructors end? Does virtual inheritance change anything?

+66
c ++ constructor destructor
Apr 18 2018-12-18T00:
source share
3 answers

if the constructor throws an exception, which destructors are triggered?

Destructors of all objects completely created in this area.

Does it matter if the exception is during the initialization list or body?

All completed objects will be destroyed.
If the constructor was never fully invoked, the object was never built and therefore could not be destroyed.

what about inheritance and members? Presumably, all completed structures are destroyed. If only some members are created, delete only those who destroy them? If multiple inheritance exists, do all the destroyed constructors end? Does virtual inheritance change anything?

All completed structures are destroyed. Yes, only completely created objects are destroyed.

Good reading:

Constructor Failure from Herb Sutter

Especially, love the part where he explains:

In biological terms, a concept arose - the designer began - but, despite all efforts, he was followed by a miscarriage - the designer never approached the terms (ination).

By the way, this is why the destructor will never be called if the constructor is unsuccessful - there is nothing to destroy. "He cannot die because he never lived." Note that this makes the phrase "an object whose constructor throws an exception" really an oxymoron. Such a thing is even smaller than the ex-object ... he never lived, never was, never breathed first.

+62
Apr 18 '12 at 15:35
source share

In C ++, if a constructor throws an exception, which destructors are triggered?

All objects that have constructors end.

In particular, does any value matter if the exception is during the initialization list or body?

No. All members that are fully built before the exception will run their destructors. The member who dropped out during construction and all other non-built members will not work with their destructors. The order in which the members are constructed is defined correctly and, therefore, you know exactly what will happen if you know the point of exception throwing.

Also, what about inheritance and members?

The same rule is used.

Presumably, all completed structures are destroyed.

Yes

If only some elements are created, delete only those who destroy them?

Yes

If multiple inheritance exists, are all completed constructors executed?

Yes

Is virtual inheritance changed?

No.
But note: virtual inheritance affects the order called by the constructors. If you are not familiar with how the order is determined, this can be unintuitive until you find the exact rules.

+17
Apr 18 2018-12-18T00:
source share

Any object created in the local area remaining due to the constructor will be destroyed. Runtime processing backs up the stack, invoking destructors until it finds a handler.

If an exception is thrown from the constructor, we will call the destructors of all fully constructed subobjects. Also, if the constructor was part of the new expression, the corresponding placement of the delete operator will be called if it exists.

+4
Apr 18 2018-12-18T00:
source share



All Articles