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

James Dennett jdennett at acm.org
Fri Dec 3 00:17:58 EST 2004


On Thursday, December 2, 2004, at 08:35 AM, Hammond, Peter wrote:

>
>> Hello all,
>>
>>
>> Summary for ITEM 12: Exception Safety: Is it Worth it?
>>
>>
>> The author answers the question on whether it is worth writing
>> exception-safe code or not.
>>
>>
> <snip>
>> The discussion then addresses why it is good to write code that
>> satisfies at least one of these guarantees and which of them should be
>> targeted depending on the situation.
>>
>
> The article suggests, nay states, that all code needs to be exception 
> safe.
> This only true where the application needs to carry on after an 
> exception.
> This depends on what exceptions are used for of course, but in my 
> experience
> they are used to report very bad things in libraries, and the 
> application
> tells the user and gives up. In that case it doesn't much matter if it 
> leaks
> memory or other resources, or classes become unstable, because they 
> are not
> called any more.

The conclusion doesn't follow if, as many applications do, you have
persistent resources which won't be cleaned up by the OS.

>
> Now I know this is a very simplistic view of the world, but it works. 
> I read
> thse articles and they make perfect sense, but then I try to apply 
> them to
> the day job and find that we are not going to continue anyway so it 
> would be
> a waste of effort.
>

If you just have things in memory, then it's fine to just terminate the
program with no cleanup, and in some cases (say, assertion failures)
that's often the right thing to do -- but an exception being thrown 
should
not ordinarily by so terrible that an application can't clean up 
properly.

> So how do you justify either using exceptions or not using them?
>
It depends on many factors, as do most such decisions.

-- James




More information about the Effective-cpp mailing list