Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > What is "finally:" for?

Reply
Thread Tools

What is "finally:" for?

 
 
Max Ivanov
Guest
Posts: n/a
 
      09-16-2008
Hi all!
When and where I should use try-except-finally statement? What is the
difference between:
--------
try:
A...
except:
B....
finally:
C...
--------

and
-------
try:
A...
except:
B....
C...
 
Reply With Quote
 
 
 
 
Andreas Kaiser
Guest
Posts: n/a
 
      09-16-2008
On 16 Sep., 13:25, "Max Ivanov" <(E-Mail Removed)> wrote:
> Hi all!
> When and where I should use try-except-finally statement? What is the
> difference between:
> --------
> try:
> * A...
> except:
> * B....
> finally:
> * C...
> --------

It does A if no exception or B if an exception occurs. THEN it does
always C.
>
> and
> -------
> try:
> * A...
> except:
> * B....
> C...

If an exception occurs, C is never reaching.

Andreas
 
Reply With Quote
 
 
 
 
Andreas Kaiser
Guest
Posts: n/a
 
      09-16-2008
On 16 Sep., 13:25, "Max Ivanov" <(E-Mail Removed)> wrote:
> Hi all!
> When and where I should use try-except-finally statement? What is the
> difference between:
> --------
> try:
> * A...
> except:
> * B....
> finally:
> * C...
> --------
>
> and
> -------
> try:
> * A...
> except:
> * B....
> C...


Look at: http://docs.python.org/ref/try.html and http://bytes.com/forum/thread24648.html

The python doc says:
"Changed in version 2.5: In previous versions of Python,
try...except...finally did not work. try...except had to be nested in
try...finally."

Andreas
 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 6:25*am, "Max Ivanov" <(E-Mail Removed)> wrote:
> Hi all!
> When and where I should use try-except-finally statement? What is the
> difference between:
> --------
> try:
> * A...
> except:
> * B....
> finally:
> * C...
> --------
>
> and
> -------
> try:
> * A...
> except:
> * B....
> C...


If B raises or rethrows another exception, finally: ensures that C
still gets called. In the second form that you give, C would not get
called. Here is one way to use try-except-finally:

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


If you are allocating a resource, lock, file, database, etc., then
exception-safe coding style is to get the resource in a try: and
release the resource in finally:

-- Paul
 
Reply With Quote
 
Ant
Guest
Posts: n/a
 
      09-16-2008
On Sep 16, 12:30*pm, Andreas Kaiser <(E-Mail Removed)> wrote:
> On 16 Sep., 13:25, "Max Ivanov" <(E-Mail Removed)> wrote:> Hi all!
> > When and where I should use try-except-finally statement? What is the
> > difference between:
> > --------
> > try:
> > * A...
> > except:
> > * B....
> > finally:
> > * C...
> > --------

>
> It does A if no exception or B if an exception occurs. THEN it does
> always C.


Rather it runs the code in A. If an exception is thrown in A, B is
then run. C is run regardless.

> > -------
> > try:
> > * A...
> > except:
> > * B....
> > C...

>
> If an exception occurs, C is never reaching.


Whether or not C runs will depend on what code is present in B (e.g.
it could return, or throw a different exception).

Typically you will use a finally clause if there is some cleanup that
should occur regardless of whether an exception is thrown. For
example:

def getNumbers(filename):
numbers = []

fh = open("test.txt") # Should contain lines of numbers
try:
for line in fh:
if line.strip():
numbers.append(int(line.strip()))
except ValueError, e:
return None
finally:
fh.close()

return numbers


This ensures that open files are closed for example.

--
Ant.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments