catching all exceptions

Tomasz Lisowski tomlis at notmyisp.pl
Sat Aug 13 15:24:02 EDT 2005


> Hello,
> 
> I'd like to catch all exeptions and be able to inspect them.
> 
> The simple case: I know which exceptions I'll get:
> 
> # standard textbook example:
> try:
>     something()
> except ThisException, e:
>     print "some error occurred: ", str(e)
> 
> 
> The not-so-simple case: Handling all other exceptions:
> 
> # nice-to-have:
> try:
>     something()
> except *, e:
>     print "some error occurred: ", type(e), str(e)
> 
> 
> Well, actually the second statement doesn't even compile... any ideas
> why I shouldn't be able to catch "anonymous" exceptions like this, or
> whether and how I can (and only overlooked it)?
> 
> 
> TIA!
> 
> 
> Kind Regards,
> Toni

Try this:

import sys

try:
   something()
except:
   info = sys.exc_info()
   ...

and you can inspect the tuple info, which contains the exception type, 
value, and traceback.

Best regards,
Tom



More information about the Python-list mailing list