The difficulty in writing exception safe code isn't in writing the code that throws an exception, or in writing the code that catches the exception to handle it; it is in writing the code in between.
How do we want such code to behave? And how do we write it to behave that way? I’m going to deal with both of these questions in turn.
Imagine for a moment the call stack of a running program, function a() has called function b(), b() has called c(), and so on, until we reach x() which encounters a problem and throws an exception. This exception causes the stack to “unwind”, deleting automatic variables along the way, until the exception is caught and dealt with by a().
We need to write each of these functions so that something sensible happens as a result of this process. If we can achieve this we have “exception safe” code.