Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > catching all exceptions

Reply
Thread Tools

catching all exceptions

 
 
a_geek@web.de
Guest
Posts: n/a
 
      08-13-2005
Hello,

I'd like to catch all exeptions and be able to inspect them.

The simple case: I know which exceptions I'll get:

# standard textbook example:
try:
something()
except ThisException, e:
print "some error occurred: ", str(e)


The not-so-simple case: Handling all other exceptions:

# nice-to-have:
try:
something()
except *, e:
print "some error occurred: ", type(e), str(e)


Well, actually the second statement doesn't even compile... any ideas
why I shouldn't be able to catch "anonymous" exceptions like this, or
whether and how I can (and only overlooked it)?


TIA!


Kind Regards,
Toni
 
Reply With Quote
 
 
 
 
Paolino
Guest
Posts: n/a
 
      08-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello,
>
> I'd like to catch all exeptions and be able to inspect them.
>
> The simple case: I know which exceptions I'll get:
>
> # standard textbook example:
> try:
> something()
> except ThisException, e:
> print "some error occurred: ", str(e)
>
>
> The not-so-simple case: Handling all other exceptions:
>
> # nice-to-have:
> try:
> something()
> except *, e:
> print "some error occurred: ", type(e), str(e)
>
>

except Exception:# catch them all.

Then use moudule 'traceback' to inspect

Paolino





___________________________________
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
http://mail.yahoo.it
 
Reply With Quote
 
 
 
 
Bengt Richter
Guest
Posts: n/a
 
      08-13-2005
On Sat, 13 Aug 2005 17:42:00 +0200, (E-Mail Removed) wrote:

>Hello,
>
>I'd like to catch all exeptions and be able to inspect them.
>
>The simple case: I know which exceptions I'll get:
>
># standard textbook example:
>try:
> something()
>except ThisException, e:
> print "some error occurred: ", str(e)
>
>
>The not-so-simple case: Handling all other exceptions:
>
># nice-to-have:
>try:
> something()
>except *, e:
> print "some error occurred: ", type(e), str(e)
>
>
>Well, actually the second statement doesn't even compile... any ideas
>why I shouldn't be able to catch "anonymous" exceptions like this, or
>whether and how I can (and only overlooked it)?
>
>
>TIA!
>


>>> def test(something):

... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
...
>>> test(lambda: 1/0)

ZeroDivisionError: integer division or modulo by zero
>>> test(lambda: unk)

NameError: global name 'unk' is not defined
>>> test(lambda: open('unk'))

IOError: [Errno 2] No such file or directory: 'unk'

All exceptions should derive from Exception, so the above should catch them,
although there are deprecated string exceptions that will not be caught.
You can catch these with a bare except, but it is probably better not to,
so you will know there's something to clean up.

If you do have to deal with them, you can then catch them by name individually,
or all with the bare except, e.g.,

>>> def strexraiser(s): raise s

...
>>> def test(something):

... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
... except 'ugh':
... print 'Caught "ugh"'
... except:
... print sys.exc_info()
...
>>> import sys # forgot, will need when above executes
>>>
>>> test(lambda:strexraiser('ugh'))

Caught "ugh"
>>> test(lambda:strexraiser('gak'))

('gak', None, <traceback object at 0x02EF0ACC>)
>>> test(lambda:1/0)

ZeroDivisionError: integer division or modulo by zero

Regards,
Bengt Richter
 
Reply With Quote
 
Tomasz Lisowski
Guest
Posts: n/a
 
      08-13-2005
> Hello,
>
> I'd like to catch all exeptions and be able to inspect them.
>
> The simple case: I know which exceptions I'll get:
>
> # standard textbook example:
> try:
> something()
> except ThisException, e:
> print "some error occurred: ", str(e)
>
>
> The not-so-simple case: Handling all other exceptions:
>
> # nice-to-have:
> try:
> something()
> except *, e:
> print "some error occurred: ", type(e), str(e)
>
>
> Well, actually the second statement doesn't even compile... any ideas
> why I shouldn't be able to catch "anonymous" exceptions like this, or
> whether and how I can (and only overlooked it)?
>
>
> TIA!
>
>
> Kind Regards,
> Toni


Try this:

import sys

try:
something()
except:
info = sys.exc_info()
...

and you can inspect the tuple info, which contains the exception type,
value, and traceback.

Best regards,
Tom
 
Reply With Quote
 
a_geek@web.de
Guest
Posts: n/a
 
      08-13-2005
Hello,

Tomasz Lisowski wrote:
>> Well, actually the second statement doesn't even compile... any ideas
>> why I shouldn't be able to catch "anonymous" exceptions like this, or
>> whether and how I can (and only overlooked it)?


[ one of three suggestions: ]

> Try this:


Ok, so the answer simply was that I didn't see "it", although the
solution is in the manual.

Thank you!


Kind Regards,
Toni
 
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
Catching unhandled exceptions using HttpModules Amil Hanish ASP .Net 0 04-12-2006 11:51 PM
Catching exceptions in JSP when they occur in Servlets Mick Java 0 08-06-2003 01:12 PM
Re: Catching exceptions that are never thrown Adam Maass Java 5 07-22-2003 10:58 PM
Re: Catching exceptions that are never thrown Mike Schilling Java 2 07-16-2003 08:36 PM
Re: catching exceptions from web user controls Marina ASP .Net 2 07-08-2003 04:48 PM



Advertisments