Ashish Ashish - 3 months ago 8
C++ Question

Why does my program terminate when an exception is thrown by a destructor?

I am not getting why if there is an active exception then if an exception is raised again, it leads to termination of program. Could someone explain?

Answer

What is it suppose to do? It can't "double catch" or anything, nor does it make sense to simply ignore one. The standard specifies that if, during stack unwinding, another exception escapes, then terminate shall be called.

There is more discussion in the C++ FAQ. One "solution" is to wrap your destructor code in a try/catch block, and simply don't let exceptions escape.

Another is to come up with some sort of custom exception chaining scheme. You'd do the above, but instead of ignoring an exception, you would append it to the currently thrown exception, and at the catch site handle both by hand.

The best solution, I think, it to try to remove the exceptional code from your destructor.