Re: Current exception

Guido.van.Rossum@cwi.nl
Mon, 11 Oct 1993 11:10:42 +0100

> In an exception handler, how does one find out what the exception being
> handled was? Is it possible? I'd like to write something like:
>
> try:
> foo()
> except:
> something(e)
>
> where e is the specific exception.

Ah, that's an easy one, though I agree that the docs for this aren't
where you have probably been looking for them. The module sys defines
three variables: exc_type, exc_value and exc_traceback, which are set
to the current exception when an except clause is entered.

Note the distinction with the three similar variables last_type, ...
which are only set when the exception isn't handled and the
interpreter prints a stack trace. (This done so you can then import
the debugger and use it for post-mortem browsing. Also see the -i
option to the interpreter.)

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>