Fog Creek Software
g
Discussion Board




Exceptions

Hi Joel,

what was the background for your one-paragraph statement on Exceptions (Oct 15th), it seems to come out of thin air and disappear the same way...
was there a longer article to accompany this?

Kristofer

Kristofer Skaug
Wednesday, October 15, 2003

... oh nevermind, I found your original comments from earlier now.

In my opinion, exceptions are of great value, as long as you properly use try-finally clauses to protect your resources. It's about separating the nominal code path from the uncountable zillions of possible non-nominal code paths, with a single elegant sweep. But it's a double-edged sword, as an uncaught exception in the wrong place can wreak havoc on your program.

I think Anders Hejlsberg's comment is very appropriate:
from: http://www.artima.com/intv/handcuffs3.html

"... there's certainly tremendous value in knowing what exceptions can get thrown, and having some sort of tool that checks. [...] I think we can certainly do a lot with analysis tools that detect suspicious code, including uncaught exceptions, and points out those potential holes to you".

I would *love* to have a design time tool like that, a compiler-assisted analysis that tells me where my exception handling is demonstrably insufficient.

Kristofer

Kristofer Skaug
Wednesday, October 15, 2003

And also this one  http://www.artima.com/intv/solid.html 

But personally I agree with Joel, use error code or exception, it's kind of tradeoff.

zhy
Wednesday, October 15, 2003

That link should be http://www.artima.com/intv/solidP.html

Whilst I agree that there is a tradeoff between throwing exceptions and passing return codes, the one thing I can guarantee when I throw an exception is that it will be caught and handled, or it will bring the system to a halt *immediately*. An unchecked return code may bring the system to a halt *eventually*, but that will happen miles from where the error occurred, after potentially damaging lots of data on the way.

Breandán Dalton
Wednesday, October 15, 2003


Hmm... did I just hear someone say that exceptions are a benefit to the sloppy coder?

Joe AA
Wednesday, October 15, 2003

There are good points and bad to exceptions.  I approach them as an Ada instructor taught me: exceptions are for exceptional conditions; if something bad happens pretty frequently, it should be dealt with by the result.
Keeping the two areas separate has some special advantages.  First, planning for error returns early usually leads me to stronger designs.  Second, I know by experience that not all problems are due to coding errors and omissions.  I once spent two weeks tracing a code problem that turned out to be the combination of two incompatible libraries stomping on each other's data.  The breakthrough came by analyzing what the program was usually trying to do when the uncaught exceptions were thrown.
Another very useful rule that instructor gave me was that an exception should be handled at the lowest level where all of the necessary information was available.  A properly paranoid programmer should be ready to re-throw an exception from his handler if it doesn't fit his assumptions.
I guess my major point is that exceptions are useful, but expensive, and should be used with informed judgement.

Fred Glauner
Wednesday, October 15, 2003

*  Recent Topics

*  Fog Creek Home