Skip to content

Why destructors should not throw

Question

Is it acceptable for a C++ destructor to throw an exception? Why or why not?

Short answer

No, avoid it. Destructors run during stack unwinding; if one throws while another exception is already in flight, the program calls std::terminate. Since C++11 destructors are implicitly noexcept, so a throw terminates anyway. Catch and handle errors inside the destructor, or expose a separate close function that can report failure.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q7.5, p.226; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question