What is "finally:" for?

Scott David Daniels Scott.Daniels at Acm.Org
Tue Sep 16 22:14:54 EDT 2008


Paul McGuire wrote:
...
> try:
>     open database
>     do database stuff
> except DatabaseException, de:
>     log exception
>     throw
> finally:
>     close database
> 
> Or
> 
> try:
>     transaction = new database transaction
>     do database stuff
>     do more database stuff
>     commit transaction
>     transaction = None
> except DatabaseException, de:
>     log exception
>     throw
> finally:
>     if transaction:
>         rollback transaction

Generally it is a good idea to try the advice you are about to offer.

"raise" is the Python way to do the Lisp/Scheme "throw" which will
likely produce another error, but not the one you want.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list