Exception Handling in Python

Quinn Dunkan quinn at zloty.ugcs.caltech.edu
Tue Oct 17 16:35:13 EDT 2000


On Tue, 17 Oct 2000 06:10:29 GMT, Suchandra Thapa
<ssthapa at harper.uchicago.edu> wrote:
>    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:
>    ...

Other people have pointed out the argumentless 'except' and 'raise', but if
what you want to do is some "cleanup", you may really want 'finally':

try:
    try:
        ...
    except Foo:
        ...
    except Bar:
        ...
    else:
        ...
finally:
    ...

Of course, this example is different from yours since finally will be executed
even if no exception is raised.

I'm not really sure why python doesn't allow

try: ...
except: ...
finally: ...

but it doesn't.



More information about the Python-list mailing list