Does Python do try: except: finally: ?

Dan Bishop danb_83 at yahoo.com
Wed Mar 19 22:08:29 EST 2003


"Ulrich Petri" <ulope at gmx.de> wrote in message news:<b5b0de$253ft3$1 at ID-67890.news.dfncis.de>...
> "Skip Montanaro" <skip at pobox.com> schrieb im Newsbeitrag
> news:mailman.1048112434.6123.python-list at python.org...
> >
> > Maybe, but that's not what try/finally does.  Try/finally says, "Execute
>  the
> > code in the try block, and irregardless of any exceptions which might get
> > raised, execute the code in the finally block when finished."
> > Try/except/else says, "Execute the code in the try block.  If an exception
> > is raised, execute the code in the except block, otherwise execute the
>  code
> > in the else block."
> >
> 
> I allways asked myself of what practical use finally (except the syntactical
> "clearness of code") is?
> I can simply write like:
> 
> try:
>     do_this()
> except:
>     print "error"
> #now here is what will be executed next, so what for i do need finally?

"finally" does affect control flow.

>>> def foo():
...    try:
...       return "spam"
...    finally:
...       print "finally"
...
>>> print foo()
finally
spam




More information about the Python-list mailing list