How can I obtain the exception object on a generlized except statement?

C

Chris Allen

I am confused on one aspect of exception handling. If you specify the
exception object type to match in an except statement it is possible
to also obtain the exception object itself, but I can't figure out how
to get the exception object when I don't specify a match.

for example:

this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.

this won't work because it will think msg is the type to match

syntax error

syntax error

Hmm I didn't try that one before I started this post. It doesn't give
me a syntax error. I'll experiment.
Even if the above syntax works, is this the way to do this? It seems
sort of funky.

How do I do this? Thanks.
 
D

Diez B. Roggisch

Chris said:
I am confused on one aspect of exception handling. If you specify the
exception object type to match in an except statement it is possible
to also obtain the exception object itself, but I can't figure out how
to get the exception object when I don't specify a match.

for example:


this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.


this won't work because it will think msg is the type to match


syntax error


syntax error


Hmm I didn't try that one before I started this post. It doesn't give
me a syntax error. I'll experiment.
Even if the above syntax works, is this the way to do this? It seems
sort of funky.

How do I do this? Thanks.

pydoc sys.exc_info


Diez
 
D

Duncan Booth

Chris Allen said:
I am confused on one aspect of exception handling. If you specify the
exception object type to match in an except statement it is possible
to also obtain the exception object itself, but I can't figure out how
to get the exception object when I don't specify a match.

In most cases you can just catch Exception. If you want to be sure to
catch deprecated string exceptions also then use sys.exc_info().

try:
... something ...
except Exception, e:
print e

Also what you want to do with it when you've caught it makes a
difference:

If you are planning on logging a stack backtrace then you'll want the
traceback as well as the exception, so sys.exc_info() might be indicated
except that in that case you'll be better off using logging.exception()
instead.

try:
... something ...
except:
logging.exception("Unexpected error")

If you are just planning on swallowing the exception then you don't want
any of these: you want to catch the specific exceptions that you expect
to be raised.

Using an empty tuple for the exception specification won't catch any
exceptions. Not very useful if the tuple is a literal, but it could be
useful in some obscure situations (e.g. an except clause to handle
exceptions declared in a specific DLL which might not always be
present).
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top