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?
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.