By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,141 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,141 IT Pros & Developers. It's quick & easy.

How can I catch all exception in python?

P: n/a
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) {
...
}

Mar 27 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 27, 1:09 pm, "ying...@gmail.com" <ying...@gmail.comwrote:
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

Mar 27 '07 #2

P: n/a
En Tue, 27 Mar 2007 15:09:18 -0300, yi*****@gmail.com <yi*****@gmail.com>
escribió:
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) {
...
}
See the Further Reading section on that same page.
Exceptions are covered in the Python Tutorial here:
http://docs.python.org/tut/node10.html

--
Gabriel Genellina

Mar 27 '07 #3

P: n/a
On Mar 27, 9:15 pm, kyoso...@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)

Mar 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.