about try statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shi Mu

    #1

    about try statement

    very hard for me to understand the difference between
    try...except and try...finally
  • Sybren Stuvel

    #2
    Re: about try statement

    Shi Mu enlightened us with:[color=blue]
    > very hard for me to understand the difference between try...except
    > and try...finally[/color]

    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

    Comment

    • Bengt Richter

      #3
      Re: about try statement

      On Sat, 12 Nov 2005 10:20:08 +0100, Sybren Stuvel <sybrenUSE@YOUR thirdtower.com. imagination> wrote:
      [color=blue]
      >Shi Mu enlightened us with:[color=green]
      >> very hard for me to understand the difference between try...except
      >> and try...finally[/color]
      >
      >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.
      >[/color]
      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.,
      [color=blue][color=green][color=darkred]
      >>> try:[/color][/color][/color]
      ... 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
      ZeroDivisionErr or: integer division or modulo by zero

      Or to make the point about return[color=blue][color=green][color=darkred]
      >>> def foo(den):[/color][/color][/color]
      ... 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'
      ...[color=blue][color=green][color=darkred]
      >>> foo(0)[/color][/color][/color]
      exception can not bypass finally
      ZeroDivisionErr or: integer division or modulo by zero
      'succeeded in returning something'[color=blue][color=green][color=darkred]
      >>> foo(1)[/color][/color][/color]
      exception can not bypass finally
      1

      Regards,
      Bengt Richter

      Comment

      Working...