How to except the unexpected?

James Stroud jstroud at ucla.edu
Sat Mar 4 22:19:29 CET 2006


Rene Pijlman wrote:
> 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()
> 

Why catch an error only to re-raise it?

This falls under http://c2.com/cgi/wiki?YouReallyArentGonnaNeedThis



More information about the Python-list mailing list