what exception is it?

M

Maurice LING

Hi,

I'm trapping exceptions with these codes...

try:
[do something]
except IOError: [IO error handling]
except TypeError: [Type error handling]
except: [other error handling]

in [other error handling], one of the things I need to do is to display
to the use the kind of exception raised and the message, if any.

How can I do this without a big if statement to go through the list of
exceptions?

Thanks in advance.

Maurice
 
J

Josiah Carlson

try:
[do something]
except IOError: [IO error handling]
except TypeError: [Type error handling]
except: [other error handling]

in [other error handling], one of the things I need to do is to display
to the use the kind of exception raised and the message, if any.

How can I do this without a big if statement to go through the list of
exceptions?

import traceback

try:
#stuff
#other except statements
except:
traceback.print_exc()



- Josiah
 
E

Eric Brunel

Maurice said:
Hi,

I'm trapping exceptions with these codes...

try:
[do something]
except IOError: [IO error handling]
except TypeError: [Type error handling]
except: [other error handling]

in [other error handling], one of the things I need to do is to display
to the use the kind of exception raised and the message, if any.

How can I do this without a big if statement to go through the list of
exceptions?

Thanks in advance.

Maurice

In an except block, the raised exception is available via the function
exc_info() in module sys. So doing:

import sys
try:
...
except:
excClass, excInst, tb = sys.exc_info()

will get you the execption class (a.k.a type) in excClass and the actual
instance (a.k.a value) in excInst; tb will be a traceback object that you can
handle via the traceback module. All this is detailed in the library reference.

HTH
 

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,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top