Explains what happens when a C++ destructor throws an exception, covering three scenarios: the default case where destructors are implicitly noexcept(true) and throwing calls std::terminate, the explicit noexcept(false) case where exceptions can propagate normally, and the dangerous case where a destructor throws during stack unwinding (which always calls std::terminate regardless of noexcept specification). Includes working code examples for each scenario and concludes that destructors should not throw, with safer alternatives like logging or storing error state.

5m read timeFrom sandordargo.com
Post cover image
Table of contents
The role of a destructorWhat happens when an exception is thrownWhat if a destructor throws with no other active exception?What if a destructor throws while another exception is active?ConclusionConnect deeper

Sort: