How to except the unexpected?

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Sat Mar 4 06:58:01 EST 2006


Steven D'Aprano:
>ExpectedErrors = (URLError, IOError)
>ErrorsThatCantHappen =
>
>try:
>    process_things()
>except ExpectedErrors:
>    recover_from_error_gracefully()
>except ErrorsThatCantHappen:
>    print "Congratulations! You have found a program bug!"
>    print "For a $327.68 reward, please send the following " \
>    "traceback to Professor Donald Knuth."
>    raise
>except:
>    print "An unexpected error occurred."
>    print "This probably means the Internet is broken."
>    print "If the bug still occurs after fixing the Internet, " \
>    "it may be a program bug."
>    log_error()
>    sys.exit()

Yes, I think I'll do something like this. Perhaps combined with Peter's
advice to not micromanage, like so:

Reraise = (LookupError, ArithmeticError, AssertionError) # And then some

try:
    process_things()
except Reraise:
    raise
except:
    log_error()

-- 
René Pijlman



More information about the Python-list mailing list