Understanding and dealing with an exception

Terry Reedy tjreedy at udel.edu
Sun Oct 14 06:06:16 EDT 2012


On 10/14/2012 4:20 AM, Mark Lawrence wrote:

> You've already had some advice so I'll just point out that a bare except
> is a bad idea as you wouldn't even be able to catch a user interrupt.
> Try (groan!) catching StandardError instead.

There are some bare except:s in the stdlib, that adding another is 
frowned on and removing one is smiled upon.

However:
 >>> StandardError
Traceback (most recent call last):
   File "<pyshell#0>", line 1, in <module>
     StandardError
NameError: name 'StandardError' is not defined

Try:
 >>> Exception
<class 'Exception'>

This catches everything except a few things like Keyboard Interrupt that 
you normally should not catch.

 >>> BaseException
<class 'BaseException'>

This catches everything, but signals that doing so is probably intentional.

-- 
Terry Jan Reedy




More information about the Python-list mailing list