This talk presents the subtle issues associated with writing correct code in the presence of exceptions. Writing exception-safe code is a must for preserving the sanity of any application. This task is daunting because exceptions introduce hidden control flows and require a modified way of thinking. Several approaches to exception safety are discussed on a concrete example, with their advantages and drawbacks. In the end an useful helper class template ScopeGuard is introduced, that enables you to write exception-safe code that’s terse, easy to understand, and easy to maintain.
“I presented this to someone who hadn’t worked with exceptions, and in the end he said: ‘Cool. Exceptions are not hard, supporting them is trivial. Do they all do it this way?’ He was surprised to hear that most programmers do it the hard way and that ScopeGuard is cutting-edge.”
Andrei Alexandrescu is a Development Manager with RealNetworks, Inc. He is a former columnist of the C++ Report magazine, and a columnist of C/C++ Users Journal. You can reach him at firstname.lastname@example.org.