What is "finally:" for?

M

Max Ivanov

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...
 
A

Andreas Kaiser

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
 
A

Andreas Kaiser

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
 
P

Paul McGuire

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
 
A

Ant

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top