Q: Catching any exception an printing it

seanb at home.com seanb at home.com
Mon Feb 21 11:18:34 EST 2000

On 21 Feb, Stuart Reynolds wrote:
> Python allows you to do,
> try:
>   if a:
>      raise SomeException()
>   else:
>      raise xyz()
> except:
>   print 'There was an error'
> but is it possible to print out the exception if you don't know what
> type it is? I'd like the above error message to be more useful, but the
> exception raised inside the try block could be anything. Is it possible
> to find the last exception raised?
> Cheers,
> Stuart

You want exc_info() in the sys module - it returns a tuple of (type,
value, traceback) for the exception currently being handled.
Sean Blakey
quine = ['print "quine =",quine,"; exec(quine[0])"']; exec(quine[0])

More information about the Python-list mailing list