How can I catch all exception in python?

irstas at gmail.com irstas at gmail.com
Tue Mar 27 14:45:54 EDT 2007


On Mar 27, 9:15 pm, kyoso... at gmail.com wrote:
> Technically speaking, you can catch all errors as follows:
>
> try:
>    # do something
> except Exception, e:
>    print e

That won't catch exceptions/errors that don't derive from
Exception class. For example a string won't be caught:

try:
   raise "foo"
except Exception, e:
   print e

But this will catch all exceptions:

try:
   raise "foo"
except:
   print sys.exc_info()

(there may be other ways I don't know of)




More information about the Python-list mailing list