How to except the unexpected?

Paul Rubin http
Sat Mar 4 06:10:22 CET 2006

Steven D'Aprano <steve at> writes:
> 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."

But this isn't good, it catches asynchronous exceptions like the user
hitting ctrl-C, which you might want to handle elsewhere.  What you
want is a way to catch only actual exceptions raised from inside the
try block.

More information about the Python-list mailing list