Why is it generally considered a bad practice to throw an exception from a destructor? What happens if an exception is thrown during stack unwinding?
Last updated
Destructors run during stack unwinding. If one throws while another exception is already propagating, C++ can't handle two at once and calls
std::terminate().Since C++11 destructors are implicitly
noexcept, so any escaping exception terminates the program even outside unwinding.Instead, catch and log errors inside the destructor, and offer a separate throwing
close()/commit()method for reporting failures.