Here's a fairly simple program. Can you see the bug? (Aside from using functions with wide contracts, which is a matter of opinion.)
That's right, we failed to invoke any cleanup. That's because when the exception handling mechanism
cannot find a handler for a thrown exception, it's
implementation-specified whether the stack is unwound. So, if we want to
invoke destructors and shut down the program cleanly, we need to wrap main()
in a try-catch block.
Oh dear. Now we've managed to invoke destructors and shutdown cleanly, we've lost a heap of useful information about the exception -
where previously we had a backtrace and a core file, now all we've got is the type and the explanatory string (what()
).