Exception-handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Odd-R.

    #1

    Exception-handling

    I have come over a strange problem regarding exceptions
    This is my code:

    try:
    #some operation
    except Exception, info:
    #some message
    except:
    #??

    When executing my code, I get to the last block here. This
    I find rather strange, because I thought Exception would catch
    all exceptions. But this is obviously not the case here.

    What is this, and how can I get a hold of what causes the exception?

    Thanks!



    --
    Har du et kjøleskap, har du en TV
    så har du alt du trenger for å leve

    -Jokke & Valentinerne
  • Diez B. Roggisch

    #2
    Re: Exception-handling

    Odd-R. wrote:
    [color=blue]
    > I have come over a strange problem regarding exceptions
    > This is my code:
    >
    > try:
    > #some operation
    > except Exception, info:
    > #some message
    > except:
    > #??
    >
    > When executing my code, I get to the last block here. This
    > I find rather strange, because I thought Exception would catch
    > all exceptions. But this is obviously not the case here.
    >
    > What is this, and how can I get a hold of what causes the exception?[/color]

    You can throw everything as an exception. I _think_ that is frowned upon
    these days, but it is there so it will happen.

    However, you can use

    import sys

    try:
    raise "foo"
    except:
    print sys.exc_info()[1]


    to get a hold on what's being caught by the exception handler.

    Diez

    Comment

    • Rene Pijlman

      #3
      Re: Exception-handling

      Odd-R.:[color=blue]
      >I thought Exception would catch all exceptions.[/color]

      Try this:

      import sys

      try:
      pass # your code here
      except:
      e = sys.exc_value()

      Either to catch everything, or to get a hold on e.

      --
      René Pijlman

      Comment

      Working...