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

Alan Kennedy alanmk at hotmail.com
Fri Nov 7 09:51:08 EST 2003


[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.   
"""

The Language Reference is very readable, not excessively formal, and
well worth a read.

http://www.python.org/doc/current/ref/ref.html

regards,

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list