How can I catch all exception in python?

kyosohma at gmail.com kyosohma at gmail.com
Tue Mar 27 14:15:54 EDT 2007


On Mar 27, 1:09 pm, "ying... at gmail.com" <ying... at gmail.com> wrote:
> I read the document here about exception handling in python:
>
> http://www.diveintopython.org/file_handling/index.html
>
> Can you please tell me how can I catch all exception in python?
> like this in Java:
> try {
>  ....
>
> } catch (Throwable t) {
>  ...
> }

Technically speaking, you can catch all errors as follows:

try:
   # do something
except Exception, e:
   print e


However, this is NOT the recommended way of handling errors. Typically
you catch only expected errors, such as when you open a file, you
check for an IOError. By catching all errors, you will learn less and
likely have hard-to-understand bugs in your program.

Mike




More information about the Python-list mailing list