What happens if an exception is thrown inside a constructor or a destructor?
Last updated
Constructor throw is safe: the object never exists, so its destructor is NOT called, but already-constructed members and bases are destroyed in reverse order.
Resources acquired raw in a constructor body leak on throw. Wrap each in an RAII member (e.g.
std::unique_ptr) so it cleans itself up.Destructor throw is dangerous: destructors are implicitly
noexcept, so an escaping exception (or a second one during unwinding) callsstd::terminate().