How do I describe a method that has no side effects if an exception is thrown during execution?

290 Views Asked by At

I just can't remember the terminology used for this and other related properties.

EDIT - Maybe such a concept doesn't exist but I remember reading something in Effective C++ (or More Effective C++) where he advocated using swaps to commit changes last in the function because the vector swap functions were guaranteed not to throw an exception.

3

There are 3 best solutions below

2
On BEST ANSWER

I think you mean to say that the function is "exception-safe". See e.g. http://en.wikipedia.org/wiki/Exception_safety.

The Wikipedia article further divides the safety into various levels. This is the one that is relevant here:

2. Commit or rollback semantics, also known as strong exception safety or no-change guarantee: Operations can fail, but failed operations are guaranteed to have no side effects so all data retain original values.

There's a reference to an STL design document that introduces exception safety and commit-or-rollback semantics: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1997/N1077.asc

0
On

You might be thinking of an exception guarantee, in particular the "strong guarantee".

0
On

Having read your edit, I suspect your title is slightly incorrect. Do you really mean it has no side effects whether or not an exception is thrown (as per the current wording) or that it has no side effects when an exception is thrown, but if the method completes without throwing an exception then it will/can have side effects? There's a pretty big difference :)

The latter is described in Accelerated C# 2008 as "exception neutrality".