Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How can I catch all exception in python?

Reply
Thread Tools

How can I catch all exception in python?

 
 
yinglcs@gmail.com
Guest
Posts: n/a
 
      03-27-2007
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) {
...
}

 
Reply With Quote
 
 
 
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 1:09 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      03-27-2007
En Tue, 27 Mar 2007 15:09:18 -0300, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)>
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

 
Reply With Quote
 
irstas@gmail.com
Guest
Posts: n/a
 
      03-27-2007
On Mar 27, 9:15 pm, (E-Mail Removed) 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)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
Exception catch-all Leslie Viljoen Ruby 4 10-01-2007 06:29 PM
How to catch all runtime exception chris C++ 4 11-15-2005 08:08 AM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM



Advertisments