[Exceptional C++ Style] Item 12: Exception Safety: Is it Worth it?

Ric Parkin ric.parkin at ntlworld.com
Thu Dec 2 09:50:25 EST 2004


From: "Paul Grenyer" <paul at paulgrenyer.co.uk>
> >  This is my summary for item 12. Not a very complex item to summarize
> > but I guess one that could generate some controversy.
> 
> Another excellent summary! Anyone got any comments. Is there anyone here who 
> doesn't think it is worth writing exception safe code?


In general I think you ought to as a matter of course, as it often leads to "better" code (even if no exceptions occur.)



Except...

- If there's no need.

Eg, you use no language/library features that can throw (recently spotted that the Exceptions and RTTI have been turned off in our code base....)

- your team is proficient in non-exception code

It can cost in time, money, and quality to learn how to do it in a new way, and if the code wasn't going to be any more reliable, you have to consider not doing it.

- it would cost too much to add

eg an old exception-unsafe codebase that would get ripped to shreds if an exception propagated. You're stuck with it.

- performance issues preclude it

Compilers might not have zero-overhead stack unwinding; copy-and-swap might add too much even in cases where there is little or no gain in safety; and measurements have shown that these are an issue

In these cases, it is quite rational to decide to not do it.



Although even in these exeptional cases, I'd advocate many exception safe techniques anyway, eg RAII, as they produce better code.


Ric


-----------------------------------------
Email provided by http://www.ntlhome.com/





More information about the Effective-cpp mailing list