exceptions considered harmful

Andrea Griffini agriff at tin.it
Sat Jun 18 08:46:57 EDT 2005


On Fri, 17 Jun 2005 20:00:39 -0400, Roy Smith <roy at panix.com> wrote:

>This sounds like a very C++ view of the world.  In Python, for example, 
>exceptions are much more light weight and perfectly routine.

The problem with exceptions is coping with partial
updatd state. Suppose you call a complex computation
routine (say a boolean operation between winged edge
data structures representing nurbs boundary of two
solids) and that you get back a "ZeroDivision"
exception... how good is the data structure now ?

Either you have some way to be able to easily *guarantee*
coherence or you're doomed. Allowing the user to continue
without being sure about what is in memory is not going
to be that helpful; the result could be that instead
of losing last ten minuts you're going to waste the
last month of user work (because the user will save
the corrupted data and will notice problems only much
later). If however you can restore the situation by a
rollback or by loading a preimage, or you know that
the operation works only *reading* two solids and
creating a new one *and* you know that it's not a problem
to drop a broken solid data structure then I think it's
ok to swallow an exception.

IMO either you know exactly what caused the exception
and how the state got influenced, or you must have thick
logical walls protecting you and allowing the problem
to not propagate (for example an RDBMS rollback facility).

With python it's sort of easy to get rollback for class
instances if the code to protect doesn't plays strange
tricks. Even C++ is powerful enough to allow that.

With C++ care must be taken to just avoid memory leaks
or other resource related problem while in python this
is rarely a problem; but the real issue with exception is
partial state update and in this python is not different.
If you really have to check every place for possible
exceptions then the exception machinery is not such
a big win compared to return codes. IMO exceptions
are nice when there are many raise and few try or
except in the code; but then either you have state
protection or swallowing an exception is taboo.

Andrea



More information about the Python-list mailing list