Exception Handling in Python

Remco Gerlich scarblac at pino.selwerd.nl
Tue Oct 17 03:01:59 EDT 2000


Suchandra Thapa <ssthapa at harper.uchicago.edu> wrote in comp.lang.python:
>     I'm trying to figure out how to catch an exception, do some 
> error handling and then reraising the same exception in python.  
> Right now, I'm doing it using the following code:
> 
> try:
>     ...
> except Foo:
>     ...
> except Bar:
>     ...
> except Exception, x:
>     ...
>     raise x
> else:
>     ...
> 
> which seems to work but I wanted to know if there are any problems with
> this method  or if there is a better way to do it. The only potential problem
> I know of right now is that user defined exception not derived from Exception
> will slip through but is that much of a problem?

'raise' without argument will re-raise the currently handled exception,
so you can just use

except:
   ...
   raise
   
at the end.

If you need info about the current exception, use sys.exc_info() in
the '...' part.



More information about the Python-list mailing list