TITLE: why terminate() when exiting destructors via exceptions (Newsgroups: comp.lang.c++.moderated) CLAMAGE: Steve Clamage Exiting a destructor via an exception is a bad idea. Destructors may be called automatically as part of exception handling, and if such a destructor exits via an uncaught exception, terminate() is called immediately. POLK: Max Polk Perhaps it is a bad idea ONLY because a bad rule exists that terminate() will be called immediately. CLAMAGE: clamage@taumet.Eng.Sun.COM (Steve Clamage), 3 Sep 1996 It's easy to call it a bad rule without suggesting an alternative. What do you suggest? Whatever you suggest as a replacement rule should be more usable and have generally better results than just calling terminate(). Here are a few things to consider. Suppose while unwinding the stack to handle exception 1, a destructor exits via exception 2. What should happen? Could we treat them as nested exceptions -- handle exception 2, then finish handling exception 1? Notice that for exception 2 we unwind the stack until the handler for exception 2 is found. That unwinding also continues the handling of exception 1, so they aren't really handled in a nested fashion. What if exception 2's handler is at an outer level compared to exception 1's? One exception or the other will be improperly handled: Either we bypass exception 1's handler and continue up the stack, or we stop short of handling exception 2. OK, now suppose exception 2's handler is encountered first -- shouldn't that be a simpler case? It means we should execute exception 2's handler when we encounter it. But we are also supposed to be unwinding the stack to find exception 1's handler. Executing some other handler in the middle of deciding what to do about the first exception doesn't seem the the obviously correct thing to do. But if we just keep unwinding the stack, the effect is to ignore exception 2 other than to execute some code of dubious value. Maybe we should assign priorities explicitly to exceptions. But how do we coordinate priorities across independent compilation units and libaries from different vendors? And we would still have to decide what to do if exceptions 1 and 2 had the same priority. The reason for the "terminate" rule is that so far no one has suggested anything that gives better results in the majority of cases.