catching all exceptions

A

a_geek

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
 
P

Paolino

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
 
B

Bengt Richter

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!
... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
... 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.,
... try:
... something()
... except Exception, e:
... print '%s: %s'% (e.__class__.__name__, e)
... except 'ugh':
... print 'Caught "ugh"'
... except:
... print sys.exc_info()
... ZeroDivisionError: integer division or modulo by zero

Regards,
Bengt Richter
 
T

Tomasz Lisowski

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
 
A

a_geek

Hello,


[ 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
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top