关于更加精辟的解说和一些原则性的指引,请参考:
1、Thinking in C++ 2nd ed Vol.2
Part 1: Building Stable Systems
>>> Uncaught exceptions
>>> Exception safety
2、Effective C++ 3rd ed
>>> Item 8: Prevent exceptions from leaving destructors
一些从书里复制来的话:
From Thinking in C++:
>>> a destructor that throws an exception or causes one to be thrown is usually a sign of poor design or sloppy coding.
>>> It turns out to be practically impossible to design exception-safe code without assuming that destructors don’t throw exceptions. Don’t let destructors throw exceptions.
>>> Don’t cause exceptions in destructors
Because destructors are called in the process of throwing other exceptions, you’ll never want to throw an exception in a destructor or cause another exception to be thrown by some action you perform in the destructor. If this happens, a new exception can be thrown before the catch-clause for an existing exception is reached, which will cause a call to terminate( ).
If you call any functions inside a destructor that can throw exceptions, those calls should be within a try block in the destructor, and the destructor must handle all exceptions itself. None must escape from the destructor.
From EC++:
>>> Things to Remember
·Destructors should never emit exceptions. If functions called in a destructor may throw, the destructor should catch any exceptions, then swallow them or terminate the program.
·If class clients need to be able to react to exceptions thrown during an operation, the class should provide a regular (i.e., non-destructor) function that performs the operation.