Catching Exceptions.

Peter Hansen peter at engcorp.com
Mon Sep 27 12:05:51 EDT 2004


Keith Bolton wrote:
> I am handling exceptions currently using try, except.  Generally I don't
> handle specific exceptions and am catching all.
> Then if an exception occurs, I would like to capture that error string.
>  However, in the documentation it seems like 
> there is not a way to get the extra str data if you are handling all
> exceptions and not specifically.  Is there?

Do generic "try/except" is a *very* bad idea, and almost never
either required, or hard to avoid.

Nevertheless, if you are doing it, you can basically just
do this to get the extra info:

try:
     # failing code
except Exception, ex:
     # use ex here just as you would have in a more specific handler
     # such as with str(ex)

The key is that all exceptions are descendents of the class
Exception, except *deprecated* exceptions that are just
strings (as in "raise 'somefailure'").  (And those should never
be used in your own code anyway.)

-Peter



More information about the Python-list mailing list