try...finally is more powerful than I thought.

Discussion in 'Python' started by Brian Kelley, Nov 6, 2003.

  1. Brian Kelley

    Brian Kelley Guest

    def res():
    try:
    a = 1
    return
    finally:
    print "do I get here?"

    res()

    outputs "do I get here?"

    I can't say why I didn't really expect this, the control flow is a
    little wierd as the function isn't really returning at the "return"
    statement but executing the bit in the finally: block and then
    returning. I think :)

    That being said, I like it a lot. How is this working internally? Does
    the finally get executed when try code block goes out of scope? This
    would happen during a return or an exception which could explain the magic.

    Brian
     
    Brian Kelley, Nov 6, 2003
    #1
    1. Advertising

  2. Brian Kelley

    Peter Hansen Guest

    Brian Kelley wrote:
    >
    > def res():
    > try:
    > a = 1
    > return
    > finally:
    > print "do I get here?"
    >
    > res()
    >
    > outputs "do I get here?"
    >
    > I can't say why I didn't really expect this, the control flow is a
    > little wierd as the function isn't really returning at the "return"
    > statement but executing the bit in the finally: block and then
    > returning. I think :)


    These results might also be of interest:

    >>> def res():

    .... try:
    .... return 1
    .... finally:
    .... return 2
    ....
    >>> res()

    2
    >>> def res():

    .... try:
    .... return 1
    .... finally:
    .... return
    ....
    >>> res()
    >>> def res():

    .... try:
    .... return 1
    .... finally:
    .... pass
    ....
    >>> res()

    1

    -Peter
     
    Peter Hansen, Nov 6, 2003
    #2
    1. Advertising

  3. Brian Kelley

    Lee Harr Guest

    >> def res():
    >> try:
    >> a = 1
    >> return
    >> finally:
    >> print "do I get here?"
    >>
    >> res()
    >>
    >> outputs "do I get here?"
    >>


    > These results might also be of interest:
    >
    >>>> def res():

    > ... try:
    > ... return 1
    > ... finally:
    > ... return 2
    > ...
    >>>> res()

    > 2
    >>>> def res():

    > ... try:
    > ... return 1
    > ... finally:
    > ... return
    > ...
    >>>> res()
    >>>> def res():

    > ... try:
    > ... return 1
    > ... finally:
    > ... pass
    > ...
    >>>> res()

    > 1



    >>> def res():

    .... print 1
    .... try:
    .... print 2
    .... return 3
    .... print 4
    .... finally:
    .... print 5
    ....
    >>> res()

    1
    2
    5
    3


    interesting.
     
    Lee Harr, Nov 6, 2003
    #3
  4. Brian Kelley

    Peter Hansen Guest

    Lee Harr wrote:
    >
    > >>> def res():

    > ... print 1
    > ... try:
    > ... print 2
    > ... return 3
    > ... print 4
    > ... finally:
    > ... print 5
    > ...
    > >>> res()

    > 1
    > 2
    > 5
    > 3
    >
    > interesting.


    Why? It just proves that "finally" works, and
    executes when the return statement is encountered.

    (The point of my examples was to show that finally can
    actually override the value that would otherwise have been
    returned. I thought that was interesting, but unfortunately
    I'm not sure what your example adds to the previous two posts.
    Maybe I'm just missing the point...)

    -Peter
     
    Peter Hansen, Nov 7, 2003
    #4
  5. Brian Kelley <> writes:

    > def res():
    > try:
    > a = 1
    > return
    > finally:
    > print "do I get here?"
    >
    > res()
    >
    > outputs "do I get here?"
    >
    > I can't say why I didn't really expect this, the control flow is a
    > little wierd as the function isn't really returning at the "return"
    > statement but executing the bit in the finally: block and then
    > returning. I think :)
    >
    > That being said, I like it a lot. How is this working internally?
    > Does the finally get executed when try code block goes out of scope?
    > This would happen during a return or an exception which could explain
    > the magic.


    Internally, and locally to one function, leaving via returning a value
    and raising an exception is pretty similar.

    Cheers,
    mwh

    --
    I'm not sure that the ability to create routing diagrams
    similar to pretzels with mad cow disease is actually a
    marketable skill. -- Steve Levin
    -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html
     
    Michael Hudson, Nov 7, 2003
    #5
  6. Brian Kelley

    Alan Kennedy Guest

    [Brian Kelley]
    > How is [try..finally] working internally? Does
    > the finally get executed when try code block goes out of scope? This
    > would happen during a return or an exception which could explain the
    > magic.


    From the Python Language Reference

    http://www.python.org/doc/current/ref/try.html

    """
    The try...finally form specifies a `cleanup' handler. The try clause
    is executed. When no exception occurs, the finally clause is executed.
    When an exception occurs in the try clause, the exception is
    temporarily saved, the finally clause is executed, and then the saved
    exception is re-raised. If the finally clause raises another exception
    or executes a return or break statement, the saved exception is lost.
    A continue statement is illegal in the finally clause. (The reason is
    a problem with the current implementation - this restriction may be
    lifted in the future). The exception information is not available to
    the program during execution of the finally clause.
    """

    The Language Reference is very readable, not excessively formal, and
    well worth a read.

    http://www.python.org/doc/current/ref/ref.html

    regards,

    --
    alan kennedy
    -----------------------------------------------------
    check http headers here: http://xhaus.com/headers
    email alan: http://xhaus.com/mailto/alan
     
    Alan Kennedy, Nov 7, 2003
    #6
  7. Brian Kelley

    Ype Kingma Guest

    Jython once had a bug where it would not execute the outer finally
    through a return, ie. in:

    def x():
    try:
    try:
    return 1
    finally:
    doSomething1()
    finally:
    doSomething2()

    doSomething2() would not get executed.
    The workaround was straightforward: save the return value in some local
    variable and return at the end.

    In case you like such puzzles, this is the test code:

    http://cvs.sourceforge.net/viewcvs.py/jython/bugtests/test371.py?rev=1.2&view=markup

    Btw. CPython never failed a single test case in there.

    Have fun,
    Ype


    email at xs4all.nl
     
    Ype Kingma, Nov 7, 2003
    #7
  8. Alan Kennedy wrote:

    > When an exception occurs in the try clause, the exception is
    > temporarily saved, the finally clause is executed, and then the saved
    > exception is re-raised.


    I've always wondered, how does one use this in real life?
    Since I cannot write try... except... finally..., which would be the
    most intuitive construct to me, I understand I should write
    try:
    try:
    ...
    finally:
    ...
    except:
    ...

    to achieve the same effect.

    Am I right? I.e. (again) how do you people use try .. finally in real
    use cases?

    Does anyone else think Python could have a nicer syntax for this one?

    Mauro
     
    Mauro Cicognini, Nov 7, 2003
    #8
  9. Brian Kelley

    Peter Hansen Guest

    Mauro Cicognini wrote:
    >
    > I've always wondered, how does one use this in real life?
    > Since I cannot write try... except... finally..., which would be the
    > most intuitive construct to me, I understand I should write
    > try:
    > try:
    > ...
    > finally:
    > ...
    > except:
    > ...
    >
    > to achieve the same effect.
    >
    > Am I right? I.e. (again) how do you people use try .. finally in real
    > use cases?


    I use the above in the exceptionally rare case where I really want
    that particular behaviour: cleanup prior to unrelated exception handling.

    Normally what I want is cleanup, leaving exception handling to the calling
    code, in which case I just need a finally.

    More often I want exception handling, and don't need cleanup, so of course
    just the except clause will do.

    And only slightly more often than the first case, but much less often
    than the other two, I want exception handling but an overall cleanup,
    in which case I use the above nested format but with the finally on
    the outside and the except on the inside.

    > Does anyone else think Python could have a nicer syntax for this one?


    Perhaps, but it's such an incredibly insignificant thing as far as
    I'm concerned that after hearing of the existence of lengthy past
    discussions about this, and the near certainty it will not be changed,
    I stopped wasting my time worrying about it and went back to writing
    code that worked, and some that didn't :).

    (Basically, check the archives for the reasons this is the way it is.)

    -Peter
     
    Peter Hansen, Nov 7, 2003
    #9
  10. Brian Kelley

    Georgy Pruss Guest

    "Mauro Cicognini" <> wrote in message news:bogm9e$bn3$...
    >
    > <,,,>
    > try:
    > try:
    > ...
    > finally:
    > ...
    > except:
    > ...
    >
    > to achieve the same effect.
    >
    > Am I right? I.e. (again) how do you people use try .. finally in real
    > use cases?
    >
    > Does anyone else think Python could have a nicer syntax for this one?


    That's probably why they called it begin...rescue in Ruby :)
    G-:

    >
    > Mauro
     
    Georgy Pruss, Nov 8, 2003
    #10
  11. Brian Kelley

    Lee Harr Guest

    On 2003-11-07, Peter Hansen <> wrote:
    > Lee Harr wrote:
    >>
    >> >>> def res():

    >> ... print 1
    >> ... try:
    >> ... print 2
    >> ... return 3
    >> ... print 4
    >> ... finally:
    >> ... print 5
    >> ...
    >> >>> res()

    >> 1
    >> 2
    >> 5
    >> 3
    >>
    >> interesting.

    >
    > Why? It just proves that "finally" works, and
    > executes when the return statement is encountered.
    >
    > (The point of my examples was to show that finally can
    > actually override the value that would otherwise have been
    > returned. I thought that was interesting, but unfortunately
    > I'm not sure what your example adds to the previous two posts.
    > Maybe I'm just missing the point...)
    >



    Yes. I just found the entire thread interesting. I know that
    people who are unfamiliar with the try ... finally syntax can
    easily fire up the interpreter and play around with it for
    themselves, but I thought the "basic functionality" example
    might make the rest of the thread more clear.

    I have written a lot of python code and have never used finally.
     
    Lee Harr, Nov 8, 2003
    #11
  12. Brian Kelley

    Peter Hansen Guest

    Lee Harr wrote:
    >
    > On 2003-11-07, Peter Hansen <> wrote:
    > > Lee Harr wrote:
    > >> interesting.

    > >
    > > Why? It just proves that "finally" works, and
    > > executes when the return statement is encountered.

    >
    > Yes. I just found the entire thread interesting. I know that
    > people who are unfamiliar with the try ... finally syntax can
    > easily fire up the interpreter and play around with it for
    > themselves, but I thought the "basic functionality" example
    > might make the rest of the thread more clear.
    >
    > I have written a lot of python code and have never used finally.


    Ah, good point. :)

    -Peter
     
    Peter Hansen, Nov 8, 2003
    #12
  13. Brian Kelley

    Bob Gailer Guest

    At 07:51 AM 11/7/2003, Alan Kennedy wrote:

    >[Brian Kelley]
    > > How is [try..finally] working internally? Does
    > > the finally get executed when try code block goes out of scope? This
    > > would happen during a return or an exception which could explain the
    > > magic.

    >
    > >From the Python Language Reference

    >
    >http://www.python.org/doc/current/ref/try.html
    >
    >"""
    >The try...finally form specifies a `cleanup' handler. The try clause
    >is executed. When no exception occurs, the finally clause is executed.
    >When an exception occurs in the try clause, the exception is
    >temporarily saved, the finally clause is executed, and then the saved
    >exception is re-raised. If the finally clause raises another exception
    >or executes a return or break statement, the saved exception is lost.
    >A continue statement is illegal in the finally clause. (The reason is
    >a problem with the current implementation - this restriction may be
    >lifted in the future). The exception information is not available to
    >the program during execution of the finally clause.
    >"""


    Which is followed by """When a return, break or continue statement is
    executed in the try suite of a try...finally statement, the finally clause
    is also executed `on the way out.' A continue statement is illegal in the
    finally clause. (The reason is a problem with the current implementation --
    this restriction may be lifted in the future).""" which is what applies to
    the examples cited above.

    Bob Gailer

    303 442 2625



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.537 / Virus Database: 332 - Release Date: 11/6/2003
     
    Bob Gailer, Nov 9, 2003
    #13
  14. Peter Hansen wrote:
    > Mauro Cicognini wrote:
    >
    >>Does anyone else think Python could have a nicer syntax for this one?

    >
    > Perhaps, but it's such an incredibly insignificant thing as far as
    > I'm concerned that after hearing of the existence of lengthy past
    > discussions about this, and the near certainty it will not be changed,
    > I stopped wasting my time worrying about it and went back to writing
    > code that worked, and some that didn't :).
    >
    > (Basically, check the archives for the reasons this is the way it is.)


    Thanx for the nice explanation.

    I might check the archives... and yes, I agree it is a rather
    insignificant thing, in fact I was just curious and the mention of past
    wars does check out. I for no reason will ever wish to stir them up again.

    BR,
    Mauro
     
    Mauro Cicognini, Nov 10, 2003
    #14
    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:
    384
    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. 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
  4. Steven D'Aprano
    Replies:
    0
    Views:
    143
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    116
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page