about try statement

Discussion in 'Python' started by Shi Mu, Nov 12, 2005.

  1. Shi Mu

    Shi Mu Guest

    very hard for me to understand the difference between
    try...except and try...finally
     
    Shi Mu, Nov 12, 2005
    #1
    1. Advertising

  2. Shi Mu enlightened us with:
    > very hard for me to understand the difference between try...except
    > and try...finally


    Within a 'try' block, if an exception is called and a matching
    'except' block is found, that block will be used to handle the
    expression.

    From the documentation of the "return" keyword: "When return passes
    control out of a try statement with a finally clause, that finally
    clause is executed before really leaving the function." Note that
    there is no talk about exceptions in this.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Nov 12, 2005
    #2
    1. Advertising

  3. On Sat, 12 Nov 2005 10:20:08 +0100, Sybren Stuvel <> wrote:

    >Shi Mu enlightened us with:
    >> very hard for me to understand the difference between try...except
    >> and try...finally

    >
    >Within a 'try' block, if an exception is called and a matching
    >'except' block is found, that block will be used to handle the
    >expression.
    >
    >From the documentation of the "return" keyword: "When return passes
    >control out of a try statement with a finally clause, that finally
    >clause is executed before really leaving the function." Note that
    >there is no talk about exceptions in this.
    >

    Which is because in the finally block the exception is still "live"
    and will take effect as soon as the finally block is left, e.g.,

    >>> try:

    ... try: 1/0
    ... finally: print 'exception can not bypass finally'
    ... except Exception, e:
    ... print '%s: %s' %(e.__class__.__name__, e)
    ...
    exception can not bypass finally
    ZeroDivisionError: integer division or modulo by zero

    Or to make the point about return
    >>> def foo(den):

    ... try:
    ... try: return 1/den
    ... finally: print 'exception can not bypass finally'
    ... except Exception, e:
    ... print '%s: %s' %(e.__class__.__name__, e)
    ... return 'succeeded in returning something'
    ...
    >>> foo(0)

    exception can not bypass finally
    ZeroDivisionError: integer division or modulo by zero
    'succeeded in returning something'
    >>> foo(1)

    exception can not bypass finally
    1

    Regards,
    Bengt Richter
     
    Bengt Richter, Nov 12, 2005
    #3
    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. bienwell
    Replies:
    4
    Views:
    3,886
    bienwell
    May 27, 2005
  2. John Salerno
    Replies:
    20
    Views:
    871
    John Salerno
    Aug 11, 2006
  3. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    389
  4. =?Utf-8?B?QUo=?=

    When to TRY and not to TRY

    =?Utf-8?B?QUo=?=, Oct 16, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    342
    sloan
    Oct 17, 2007
  5. MRAB
    Replies:
    4
    Views:
    319
Loading...

Share This Page