Perl is worse!

Martijn Faassen m.faassen at vet.uu.nl
Mon Jul 31 04:56:56 EDT 2000


Steve Lamb <grey at despair.rpglink.com> wrote:
> On 30 Jul 2000 21:25:10 GMT, Martijn Faassen <m.faassen at vet.uu.nl> wrote:
>>Then have your program catch your exceptions! That's one of the main
>>reasons for the existence of the exception handling mechanism; to recover
>>gracefully from the unexpected.

>>It is possible to catch *all* exceptions, if you're worried about that.
>>Or whole groups of exceptions, if you like. They're in a hierarchy.

>     However I find that exceptions come up on the damnedest things, break you
> out of loops, cause problems all around and don't let you return to where you
> were, esp. when you use general catch all exceptions.  As Alex(?) said, you
> don't return to where you were so, to me, it seems quite impossible to be able
> to catch /all/ exceptions gracefully, ever.

Well, I have lots of working Python software still. :)

In the presence of bugs in your code, you cannot catch all exceptions
*gracefully*. This is because you do not know where all your bugs are.
Bugs are not graceful, period.

As pointed out to you before, you seem to overestimate the amount of 
exceptions you need to catch, to handle gracefully or not.

Let's try to classify exceptions:

  1. A test. You expected this exception and check for it, such as in
   
    try:
        b = int(b)
    except ValueError:
        b = 0

  2. Unexpected external conditions you didn't think of (file turned out to be
    locked, etc).
 
  3. an assumption about the structure of input that turns out to be wrong.
    i.e., a bug.

  4. a mistake in your code. You call a nonexistent function, refer to
    nonexistent variables, your algorithm is just wrong, etc. i.e., a bug.

Category 1 exceptions are no problem; you handle them. Category 2 exceptions
are the most tricky. They may hint at a bug (you should have checked whether
the file was locked). What *is* sure is that you usually cannot continue
gracefully; you can't write to the locked file and you need to, or whatever.
Ungraceful handling can be implemented with some outer exception handler,
or it can be accepted that the program can fail under these circumstances.

If it *does* turn out you can continue gracefully, you can only continue
gracefully if you catch the exception and handle it, in which case it
turns into a variety of category 1. You really fixed a bug in that case.

Note that category 1 and category 2 exceptions generally do not happen
all over your code. You factored out your code well and these things only
happen in specific places. You don't have to place handlers all over
the place.

Category 3 and 4 are bugs. Bugs cannot be handled gracefully. Again you
have two options; have the program stop in the face of a bug, or do a
catch-all and try to continue anyway.

In the light of that, let's translate what seem to be saying:

"""
     However I find that bugs come up on the damnedest things, break you
out of loops, cause problems all around and don't let you return to where you
were [in the algorithm], esp. when you use general catch all exceptions.
As Alex(?) said, you don't return to where you were so, to me, it seems quite
impossible to be able to catch /all/ bugs gracefully, ever.
"""

The answer to that is easy: No, of course not.

Regards,

Martijn 
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list