[python] using try: finally: except

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Stockwell

    [python] using try: finally: except

    In referring to my copy of the python bible, it tells me I can't use all
    three items 'try' except and finally. I can use the t/f or t/e
    combinations though

    What combination can i use if i want to catch the exception and still have a
    finally block?


    This is a fictional example of what I want....

    try:
    x = 'hello'
    except Exception, e:
    print "oops"
    finally:
    y = 'world'
    print x," ", y

    So I surmise one way to guarantee this does what I need would be to do this:

    try:
    x = 'hello'
    except Exception, e:
    print "oops"

    y = 'world'
    print x," ", y

    Wouldn't that guarantee y and the print gets executed no matter what? My
    exception catches miscellaneous errors and then processing continues.... I
    suspect that wouldn't hold for a complex case where the action in the try
    block causes a fatal error of some sort....

    David
    -------
    Tracfone: http://cellphone.duneram.com/index.html
    Cam: http://www.duneram.com/cam/index.html
    Tax: http://www.duneram.com/index.html

    _______________ _______________ _______________ _______________ _____
    Is your PC infected? Get a FREE online computer virus scan from McAfee®
    Security. http://clinic.mcafee.com/clinic/ibuy...n.asp?cid=3963


  • Russell Blau

    #2
    Re: [python] using try: finally: except

    "David Stockwell" <winexpert@hotm ail.com> wrote in message
    news:mailman.30 .1087503791.180 66.python-list@python.org ...[color=blue]
    > In referring to my copy of the python bible, it tells me I can't use all
    > three items 'try' except and finally. I can use the t/f or t/e
    > combinations though
    >
    > What combination can i use if i want to catch the exception and still have[/color]
    a[color=blue]
    > finally block?
    >
    >
    > This is a fictional example of what I want....
    >
    > try:
    > x = 'hello'
    > except Exception, e:
    > print "oops"
    > finally:
    > y = 'world'
    > print x," ", y[/color]

    try:
    try:
    x = 'hello'
    except Exception, e:
    print "oops"
    finally:
    y = 'world'
    print x," ", y

    Of course, in your example it doesn't matter because the 'except' clause
    doesn't stop execution from passing to the next line, but if you were doing
    something more fatal in the 'except' clause then (I think) you would still
    execute the 'finally' block.


    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.


    Comment

    • Carl Banks

      #3
      Re: [python] using try: finally: except

      David Stockwell wrote:[color=blue]
      > So I surmise one way to guarantee this does what I need would be to do this:
      >
      > try:
      > x = 'hello'
      > except Exception, e:
      > print "oops"
      >
      > y = 'world'
      > print x," ", y
      >
      > Wouldn't that guarantee y and the print gets executed no matter what? My
      > exception catches miscellaneous errors and then processing continues.... I
      > suspect that wouldn't hold for a complex case where the action in the try
      > block causes a fatal error of some sort....[/color]

      Not really. It's not very likely (but not impossible) for the except
      block you have here to throw an exception. But in more realistic
      situations, an exception might happen in the except block, and then
      your y='world' statement will not be executed.

      To show you that even print "oops" can throw an exception, try
      executing the following code:

      try:
      import sys
      sys.stdout = None
      x = 'hello'
      1/0
      except:
      print "oops"
      y = 'world'
      print x," ",y

      It should print a traceback, but not "hello world".

      The right way is:

      try:
      try:
      x = 'hello'
      except:
      print "oops"
      finally:
      y = 'world'
      print x," ",y


      --
      CARL BANKS http://www.aerojockey.com/software
      "If you believe in yourself, drink your school, stay on drugs, and
      don't do milk, you can get work."
      -- Parody of Mr. T from a Robert Smigel Cartoon

      Comment

      • OKB (not okblacke)

        #4
        Re: [python] using try: finally: except

        Carl Banks wrote:
        [color=blue]
        > The right way is:
        >
        > try:
        > try:
        > x = 'hello'
        > except:
        > print "oops"
        > finally:
        > y = 'world'
        > print x," ",y
        >[/color]

        I seem to recall reading somewhere that this was a cop-out for some
        implementation reason. Is there any word on when or if it's going to be
        remedied? It seems unbearably ugly and unintuitive; one of the most
        irritating Python warts.

        --
        --OKB (not okblacke)
        Brendan Barnwell
        "Do not follow where the path may lead. Go, instead, where there is
        no path, and leave a trail."
        --author unknown

        Comment

        • Peter Hansen

          #5
          Re: [python] using try: finally: except

          OKB (not okblacke) wrote:
          [color=blue]
          > Carl Banks wrote:
          >[color=green]
          >>The right way is:
          >>
          >> try:
          >> try:
          >> x = 'hello'
          >> except:
          >> print "oops"
          >> finally:
          >> y = 'world'
          >> print x," ",y[/color]
          >
          > I seem to recall reading somewhere that this was a cop-out for some
          > implementation reason. Is there any word on when or if it's going to be
          > remedied? It seems unbearably ugly and unintuitive; one of the most
          > irritating Python warts.[/color]

          I recall differently. I recall reading several times that since
          it is completely ambiguous what the programmer meant if both are
          specified together, Guido deliberately kept them separate so that
          one had to be very explicit about whether the finally was inside
          or outside the except. The behaviour of the code is quite different
          depending on the order...

          -Peter

          Comment

          • Carl Banks

            #6
            Re: [python] using try: finally: except

            Peter Hansen wrote:[color=blue]
            > OKB (not okblacke) wrote:
            >[color=green]
            >> Carl Banks wrote:
            >>[color=darkred]
            >>>The right way is:
            >>>
            >>> try:
            >>> try:
            >>> x = 'hello'
            >>> except:
            >>> print "oops"
            >>> finally:
            >>> y = 'world'
            >>> print x," ",y[/color]
            >>
            >> I seem to recall reading somewhere that this was a cop-out for some
            >> implementation reason. Is there any word on when or if it's going to be
            >> remedied? It seems unbearably ugly and unintuitive; one of the most
            >> irritating Python warts.[/color]
            >
            > I recall differently. I recall reading several times that since
            > it is completely ambiguous what the programmer meant if both are
            > specified together, Guido deliberately kept them separate so that
            > one had to be very explicit about whether the finally was inside
            > or outside the except. The behaviour of the code is quite different
            > depending on the order...[/color]


            try...except and try...finally are really two completely different
            statements with different purposes. It's very unfortunate that try is
            used for both of them.

            Frankly, when you do a try...finally, you're not really trying. In
            the words of the Jedi Master: "Try not. Do, or do not. There is no
            try." Which is why I think it should rather be do...finally.


            --
            CARL BANKS http://www.aerojockey.com/software
            "If you believe in yourself, drink your school, stay on drugs, and
            don't do milk, you can get work."
            -- Parody of Mr. T from a Robert Smigel Cartoon

            Comment

            • Peter Hansen

              #7
              Re: [python] using try: finally: except

              Carl Banks wrote:[color=blue]
              > Frankly, when you do a try...finally, you're not really trying. In
              > the words of the Jedi Master: "Try not. Do, or do not. There is no
              > try." Which is why I think it should rather be do...finally.[/color]

              I have to disagree (because it's wrong :-) :

              try:
              print "will this code work?"
              x = y
              print "no, it won't!"
              finally:
              print "so it really _was_ 'try'..."

              If you used 'do' you might get the impression that the contents
              of the 'do' were guaranteed to execute or something...

              -Peter

              Comment

              • Tim Peters

                #8
                Re: [python] using try: finally: except

                [Peter Hansen, on mixing 'except' with 'finally' clauses][color=blue]
                > I recall differently. I recall reading several times that since
                > it is completely ambiguous what the programmer meant if both are
                > specified together, Guido deliberately kept them separate so that
                > one had to be very explicit about whether the finally was inside
                > or outside the except.[/color]

                It's more that Guido deliberately separated them. Before Python
                0.9.6, you could attach both 'except' and 'finally' clauses to the
                same 'try' structure (see Misc/HISTORY in a Python source
                distribution). I don't remember the semantics, and that was indeed
                the problem: nobody could remember, and half the time guessed wrong.

                Comment

                • Peter Hansen

                  #9
                  Re: [python] using try: finally: except

                  Tim Peters wrote:
                  [color=blue]
                  > [Peter Hansen, on mixing 'except' with 'finally' clauses]
                  >[color=green]
                  >>I recall differently. I recall reading several times that since
                  >>it is completely ambiguous what the programmer meant if both are
                  >>specified together, Guido deliberately kept them separate so that
                  >>one had to be very explicit about whether the finally was inside
                  >>or outside the except.[/color]
                  >
                  >
                  > It's more that Guido deliberately separated them. Before Python
                  > 0.9.6, you could attach both 'except' and 'finally' clauses to the
                  > same 'try' structure (see Misc/HISTORY in a Python source
                  > distribution). I don't remember the semantics, and that was indeed
                  > the problem: nobody could remember, and half the time guessed wrong.[/color]

                  I'm curious: was it that the order of execution was fixed, regardless
                  of the order of the 'finally' and 'except' in the source, or was
                  it still confusing even though the order of execution changed
                  logically with the order of the statements in the source?

                  Comment

                  • Tim Peters

                    #10
                    Re: [python] using try: finally: except

                    [Tim Peters][color=blue][color=green]
                    >> It's more that Guido deliberately separated them. Before Python
                    >> 0.9.6, you could attach both 'except' and 'finally' clauses to the
                    >> same 'try' structure (see Misc/HISTORY in a Python source
                    >> distribution). I don't remember the semantics, and that was indeed
                    >> the problem: nobody could remember, and half the time guessed wrong.[/color][/color]

                    [Peter Hansen][color=blue]
                    > I'm curious: was it that the order of execution was fixed, regardless
                    > of the order of the 'finally' and 'except' in the source, or was
                    > it still confusing even though the order of execution changed
                    > logically with the order of the statements in the source?[/color]

                    If present, a 'finally' clause had to be the last clause in a
                    try/except/finally structure. That was enforced by the syntax. The
                    most common confusion was over whether the code in the 'finally'
                    clause would execute if an exception was raised during execution of an
                    'except' clause. That code isn't in the 'try' block, so why should
                    'finally' apply to it? For that matter, why shouldn't it? That's why
                    nobody could remember (and I in fact don't remember what happened
                    then).

                    Comment

                    Working...