No it’s not. Be sure to clean up anything that could need cleaning up in a finally block.
This is like saying... be sure to never copy more bytes than the buffer capacity. Easier said than done.
Writing exception safe code is very hard. Do not take my world for it. Read Alessandro Warth's paper (with Alan Kay as a co-author) [1]. Do not skip section 3...
Let me quote section 3.1:
In languages that support exception-handling mechanisms (e.g., the try/catch statement), a piece of code is said to be exception-safe if it guarantees not to leave the program in an inconsistent state when an exception is thrown. Writing exception-safe code is a tall order, as we illustrate with the following example:
try {
for (var idx = 0; idx < xs.length; idx++)
xs[idx].update();
} catch (e) {
// ...
}
Our intent is to update every element of xs, an array. The problem is that if one of
the calls to update throws an exception, some (but not all) of xs’ elements will have
been updated. So in the catch block, the program should restore xs to its previous
consistent state, in which none of its elements was updated.
One way to do this might be to make a copy of every element of the array before
entering the loop, and in the catch block, restore the successfully-updated elements to
their previous state. In general, however, this is not sufficient since update may also
have modified global variables and other objects on the heap. Writing truly exceptionsafe code is difficult and error-prone.Now, I have seen a lot of code, and very very very few times I've seen someone restoring the state of a collection after an exception blows.