What is "finally:" for?

Discussion in 'Python' started by Max Ivanov, Sep 16, 2008.

  1. Max Ivanov

    Max Ivanov Guest

    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...
     
    Max Ivanov, Sep 16, 2008
    #1
    1. Advertising

  2. On 16 Sep., 13:25, "Max Ivanov" <> 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
     
    Andreas Kaiser, Sep 16, 2008
    #2
    1. Advertising

  3. On 16 Sep., 13:25, "Max Ivanov" <> 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
     
    Andreas Kaiser, Sep 16, 2008
    #3
  4. Max Ivanov

    Paul McGuire Guest

    On Sep 16, 6:25 am, "Max Ivanov" <> 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
     
    Paul McGuire, Sep 16, 2008
    #4
  5. Max Ivanov

    Ant Guest

    On Sep 16, 12:30 pm, Andreas Kaiser <> wrote:
    > On 16 Sep., 13:25, "Max Ivanov" <> 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.
     
    Ant, Sep 16, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VB Programmer

    Question: Try,Catch,Finally

    VB Programmer, Aug 7, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    383
    Kevin Spencer
    Aug 7, 2003
  2. VB Programmer

    The problem with Try Catch Finally...

    VB Programmer, Aug 12, 2003, in forum: ASP .Net
    Replies:
    23
    Views:
    791
    Chad Myers
    Aug 15, 2003
  3. Tony Baker
    Replies:
    0
    Views:
    6,756
    Tony Baker
    Dec 17, 2003
  4. localhost
    Replies:
    1
    Views:
    397
    Steven Cheng[MSFT]
    Dec 25, 2003
  5. David Lozzi

    Try...Catch...Finally not firing finally?

    David Lozzi, Apr 23, 2007, in forum: ASP .Net
    Replies:
    12
    Views:
    821
    Alvin Bruney [MVP]
    May 11, 2007
Loading...

Share This Page