Re: Exception names

Guido.van.Rossum@cwi.nl
Wed, 01 Mar 1995 16:45:03 +0100

> Is there a way to catch any exception and get its content ? Mmm... Ok,
> I'm unclear, let's give an example:
>
> try:
> a = 1 / 0
> except ZeroDivisionError, msg:
> print "Message was", msg
>
> This will print "Message was integer division or modulo". Everything
> is fine.
>
> But how could I do something like:
>
> try:
> a = 1 / 0
> except exc, msg:
> print "Exception was", exc, "and message", msg
>
> I want to be able to assign exc to the raised exception.

The global variable (in module sys) sys.exc_type contains the
exception type. For completeness, sys.exc_value contains the
exception value (same as msg), sys.exc_traceback contains the
traceback object. These three variables are given a value whenever an
except clause catches an exception.

When an exception is not handled and the interactive Python command
prompt (">>> ") is printed, the exception information is stored in
three other variables: sys.last_type, sys.last_value and
sys.last_traceback.

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>