How can I catch all exception in python?

K

kyosohma

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
 
I

irstas

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top