[Tutor] finally
Chris
g.nius.ck at gmail.com
Thu Jun 24 16:44:05 CEST 2010
On 06/23/2010 10:59 PM, bob gailer wrote:
> On 6/23/2010 7:36 PM, Christopher King wrote:
>> In a try except clause, you can end with finally block. I know it
>> runs after the try and except blocks regardless of the outcome, but
>> why use it. Couldn't you just put the code after the try and except
>> block without using a finally block. Does the finally command do
>> something I don't know about. Does it make your program more
>> understandable in some way?
>
> Did you read the manual?
>
> "If finally <#finally> is present, it specifies a 'cleanup' handler.
> The try <#try> clause is executed, including any except <#except> and
> else <#else> clauses. If an exception occurs in any of the clauses and
> is not handled, the exception is temporarily saved. The finally
> <#finally> clause is executed. If there is a saved exception, it is
> re-raised at the end of the finally <#finally> clause. If the finally
> <#finally> clause raises another exception or executes a return
> <simple_stmts.html#return> or break <simple_stmts.html#break>
> statement, the saved exception is lost. The exception information is
> not available to the program during execution of the finally
> <#finally> clause.
>
> "When a return <simple_stmts.html#return>, break
> <simple_stmts.html#break> or continue <simple_stmts.html#continue>
> statement is executed in the try <#try> suite of a try
> <#try>...finally <#finally> statement, the finally <#finally> clause
> is also executed 'on the way out.' A continue
> <simple_stmts.html#continue> statement is illegal in the finally
> <#finally> clause. (The reason is a problem with the current
> implementation --- this restriction may be lifted in the future)."
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
for the second quote, it means that the try...finally statement, finally
will always execute even if you use return, break, or continue
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100624/6676255a/attachment-0001.html>
More information about the Tutor
mailing list