What destructors are run when the constructor throws an exception?

19,821

Solution 1

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

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

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.

Share:
19,821

Related videos on Youtube

WilliamKF
Author by

WilliamKF

Updated on June 04, 2022

Comments

  • WilliamKF
    WilliamKF almost 2 years

    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?

    • Steve Wellens
      Steve Wellens about 12 years
      Is there some reason you didn't test this yourself?
    • Ben Voigt
      Ben Voigt about 12 years
      @Steve: Because relying on the behavior of one particular compiler version is a terrible idea if you want portable or maintainable code.
    • Steve Wellens
      Steve Wellens about 12 years
      @BenVoigt - I agree in theory. But when you are coding in the real world, you must deal with reality. And I would be extremely surprised if all the major compilers didn't get this right.
  • Armen Tsirunyan
    Armen Tsirunyan about 12 years
    What on earth is a "logical scope"?
  • Jerry Coffin
    Jerry Coffin about 12 years
    You might also want to read @Als's earlier answer on roughly the same subject.
  • Spacen Jasset
    Spacen Jasset about 10 years
    +1 for description of what happens with new'd expressions. This is important.
  • C.M.
    C.M. almost 4 years