Does Python do try: except: finally: ?

Alex Martelli aleax at aleax.it
Wed Mar 19 16:24:58 EST 2003


Jerry Seutter wrote:

> Does Python do a block like:
> 
> try:
>     do_something()
> except:
>     do_something_else()
> finally:
>     do_cleanup()

No, try/except and try/finally are two separate statements.


> Python gives me an error when I try to do this and the
> online documentation only gives examples with try-except
> and try-finally blocks. To get around this I do:
> 
> try:
>     try:
>         do_something()
>     except:
>         do_something_else()
> finally:
>     do_cleanup()
> 
> Is this the way it has to be done?

This is one correct way to nest these statements, yes.


Alex





More information about the Python-list mailing list