what exception is it?

Eric Brunel eric_brunel at despammed.com
Thu Oct 14 04:58:34 EDT 2004

Maurice LING wrote:
> 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
   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.

- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

More information about the Python-list mailing list