Why does my program exit when an exception is thrown by the destructor?

I do not understand why, if there is an active exception, then if the exception occurs again, this leads to the termination of the program. Can someone explain?

+4
source share
6 answers

What should he do? He cannot "catch twice" or anything else, and there is no point simply ignoring him. The standard states that if another exception is thrown during stack expansion, then terminate is called.

In the C ++ section, the question is often asked . One β€œsolution” is to wrap your destructor code in a try / catch block and simply not allow exceptions to be thrown.

Another is to create some kind of exception chain configuration scheme. You would do the above, but instead of ignoring the exception, you would add it to the existing exception, and on the catch node handle it as manually.

The best solution, I think, is to try to remove the exclusive code from your destructor.

+14
source

The reason is simple: if an exception occurs during the propagation of exceptions, then what exception should be thrown? Original exception or new exception? If a new exception is propagated and processed, how does the program know that another exception has occurred? Or ignore the original exception? This and many other complications lead to a simple rule that only one exception can be propagated at one time, and when multiple failures the application terminates.

+3
source

Indicate the standard (15.2.3):

The process of invoking destructors for automatic objects built along the path from a try block to a throw expression is called `` stack unwinding. '' [Note: if the destructor called during the unwinding of the stack exits with an exception, terminate (except.terminate). Thus, destructors should usually catch exceptions and prevent their propagation from the destructor. --- end of note]

Basically, C ++ (like most other popular programming languages) does not have good support for handling multiple errors using exceptions. Exceptions, as a mechanism, are simply insufficient in this regard.

Frequently asked questions contains a sentence. How to handle a destructor that does not work?

Stroustroup has this to say on this subject ( TCPL 14.7 ):

The reason for terminate () is that exception handling sometimes needs to be left to less subtle error handling methods. For example, terminate () can be used to interrupt a process or, possibly, to reinitialize a system. The goal is for terminate () to be a radical measure to apply when the error recovery strategy implemented by the exception handling engine fails and it is time to move to another level of the failover strategy.

See also the previous related discussion of SO: basically any question about exceptions and destructors .

+2
source

This article has an explanation of the problem: http://web.tiscali.it/fanelia/cpp-faq-en/exceptions.html#faq-17.3

+1
source

When you throw exception, it continues to unwind the stack until it reaches the corresponding catch . As part of the process of expanding the stack, destructors are called for each object in each area of ​​the frame.

Now that the destructor throws an exception in this case, is there a dilemma - which catch is the program to stop at? Original exception or new exception? In any case, an unhandled exception is used here.

The program is not very good at making such decisions, so the standard says that it will not even try to solve the problem and will simply leave.

Check the FAQ-Lite entry explaining this exact situation for further details.

+1
source

Item 8 Effective C ++ says you should never let an exception leave a destructor.

+1
source

All Articles