weird try/finally behaviour

Discussion in 'Python' started by Sylvain Thénault, Apr 10, 2009.

  1. Hi there,

    I've encountered the following behaviour which I found surprising:

    >>> def test():

    .... for x in ('test', 'tests'):
    .... try:
    .... if True:
    .... print 'return'
    .... return 1
    .... finally:
    .... print 'break'
    .... break
    .... print 'end'
    ....
    >>>
    >>> test()

    return
    break
    end

    As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored.
    Is this a known caveat or should it be considered as a bug?
    --
    Sylvain Thénault LOGILAB, Paris (France)
    Formations Python, Debian, Méth. Agiles: http://www.logilab.fr/formations
    Développement logiciel sur mesure: http://www.logilab.fr/services
    CubicWeb, the semantic web framework: http://www.cubicweb.org
     
    Sylvain Thénault, Apr 10, 2009
    #1
    1. Advertising

  2. Sylvain Thénault

    Tim Hoffman Guest

    Hi Sylvain

    You should have a read of the python docs, specifically on try:
    finally:

    excerpt from docs. --

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

    See ya

    Tim


    On Apr 10, 7:49 pm, Sylvain Thénault <>
    wrote:
    > Hi there,
    >
    > I've encountered the following behaviour which I found surprising:
    >
    > >>> def test():

    >
    > ...     for x in ('test', 'tests'):
    > ...          try:
    > ...             if True:
    > ...                   print 'return'
    > ...                   return 1
    > ...          finally:
    > ...             print 'break'
    > ...             break
    > ...     print 'end'
    > ...
    >
    > >>> test()

    >
    > return
    > break
    > end
    >
    > As you can see, the 'break' in the finally block makes the 'return 1' beeing ignored.
    > Is this a known caveat or should it be considered as a bug?
    > --
    > Sylvain Thénault                               LOGILAB, Paris (France)
    > Formations Python, Debian, Méth. Agiles:http://www.logilab.fr/formations
    > Développement logiciel sur mesure:      http://www.logilab.fr/services
    > CubicWeb, the semantic web framework:    http://www.cubicweb.org
     
    Tim Hoffman, Apr 10, 2009
    #2
    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:
    373
    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:
    783
    Chad Myers
    Aug 15, 2003
  3. localhost
    Replies:
    1
    Views:
    393
    Steven Cheng[MSFT]
    Dec 25, 2003
  4. =?Utf-8?B?UmVwU3RhdA==?=

    try/finally with data access code

    =?Utf-8?B?UmVwU3RhdA==?=, Apr 22, 2004, in forum: ASP .Net
    Replies:
    9
    Views:
    360
    Bruno Jouhier [MVP]
    Apr 23, 2004
  5. David Lozzi

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

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

Share This Page