try...finally is more powerful than I thought.

Bob Gailer bgailer at alum.rpi.edu
Sun Nov 9 18:02:07 EST 2003


At 07:51 AM 11/7/2003, Alan Kennedy wrote:

>[Brian Kelley]
> > How is [try..finally] working internally?  Does
> > the finally get executed when try code block goes out of scope?  This
> > would happen during a return or an exception which could explain the
> > magic.
>
> >From the Python Language Reference
>
>http://www.python.org/doc/current/ref/try.html
>
>"""
>The try...finally form specifies a `cleanup' handler. The try clause
>is executed. When no exception occurs, the finally clause is executed.
>When an exception occurs in the try clause, the exception is
>temporarily saved, the finally clause is executed, and then the saved
>exception is re-raised. If the finally clause raises another exception
>or executes a return or break statement, the saved exception is lost.
>A continue statement is illegal in the finally clause. (The reason is
>a problem with the current implementation - this restriction may be
>lifted in the future). The exception information is not available to
>the program during execution of the finally clause.
>"""

Which is followed by """When a return, break or continue statement is 
executed in the try suite of a try...finally statement, the finally clause 
is also executed `on the way out.' A continue statement is illegal in the 
finally clause. (The reason is a problem with the current implementation -- 
this restriction may be lifted in the future).""" which is what applies to 
the examples cited above.

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20031109/999e0e53/attachment.html>
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.537 / Virus Database: 332 - Release Date: 11/6/2003


More information about the Python-list mailing list