What destructors are run when the constructor throws an exception?

C++ConstructorException HandlingDestructor

C++ Problem Overview


In C++, if a constructor throws an exception, what destructors are run?

In particular, does it make any difference if the exception is during the initialization list or the body?

Also, what about inheritance and members? Presumably all completed constructions get destructed. If only some members are constructed, do only those get destructed? If there is multiple inheritance, do all completed constructors get destructed? Does virtual inheritance change anything?

C++ Solutions


Solution 1 - C++

if a constructor throws an exception, what destructors are run?

Destructors of all the objects completely created in that scope.

Does it make any difference if the exception is during the initialization list or the body?

All completed objects will be destructed.
If constructor was never completely called object was never constructed and hence cannot be destructed.

what about inheritance and members? Presumably all completed constructions get destructed. If only some members are constructed, do only those get destructed? If there is multiple inheritance, do all completed constructors get destructed? Does virtual inheritance change anything?

All completed constructions do get destructed. Yes, only the completely created objects get destructed.

Good Read:

Constructor Failures by Herb Sutter

Especially, love the part where he explains:

> In biological terms, conception took place -- the constructor began --, but despite best efforts it was followed by a miscarriage -- the constructor never ran to term(ination). > > Incidentally, this is why a destructor will never be called if the constructor didn't succeed -- there's nothing to destroy. "It cannot die, for it never lived." Note that this makes the phrase "an object whose constructor threw an exception" really an oxymoron. Such a thing is even less than an ex-object... it never lived, never was, never breathed its first.

Solution 2 - C++

> In C++, if a constructor throws an exception, what destructors are run?

All objects that have had constructors run to completion.

> In particular, does it make any difference if the exception is during the initialization list or the body?

No. All members that are fully constructed prior to the exception will have their destructors run. The member that threw during construction and all other non constructed members will not have their destructors run. The order of member construction is well defined and thus you know exactly what will happen given you know the point of the exception throw.

> Also, what about inheritance and members?

Same rule applies.

> Presumably all completed constructions get destructed.

Yes

> If only some members are constructed, do only those get destructed?

Yes

> If there is multiple inheritance, do all completed constructors get destructed?

Yes

> Does virtual inheritance change anything?

No.
But note: Virtual inheritance does affect the order the constructors are called. If you are not familiar with how the order is defined this may be non intuitive until you look up the exact rules.

Solution 3 - C++

Any object created in a local scope left because of the constructor will be destructed. The runtime handling walks back up the stack, calling destructors, until it finds a handler.

If the exception is thrown from a constructor, the destructors of all completely constructed subobjects will be called. In addition, if the constructor was part of a new expression, the appropriate placement delete operator will be called if it exists.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWilliamKFView Question on Stackoverflow
Solution 1 - C++Alok SaveView Answer on Stackoverflow
Solution 2 - C++Martin YorkView Answer on Stackoverflow
Solution 3 - C++James KanzeView Answer on Stackoverflow