How can I obtain the exception object on a generlized except statement?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Allen

    How can I obtain the exception object on a generlized except statement?

    I am confused on one aspect of exception handling. If you specify the
    exception object type to match in an except statement it is possible
    to also obtain the exception object itself, but I can't figure out how
    to get the exception object when I don't specify a match.

    for example:
    >>try: urlopen('http://www.google.com' )
    >>except socket.error, msg:
    >> print str(msg)
    this works and I can do what I like with the exception object (msg).
    but I can't do this with a simple except statment.
    >>except msg:
    this won't work because it will think msg is the type to match
    >>except ,msg:
    syntax error
    >>except *,msg:
    syntax error
    >>except (),msg:
    Hmm I didn't try that one before I started this post. It doesn't give
    me a syntax error. I'll experiment.
    Even if the above syntax works, is this the way to do this? It seems
    sort of funky.

    How do I do this? Thanks.

  • Diez B. Roggisch

    #2
    Re: How can I obtain the exception object on a generlized exceptstatement ?

    Chris Allen schrieb:
    I am confused on one aspect of exception handling. If you specify the
    exception object type to match in an except statement it is possible
    to also obtain the exception object itself, but I can't figure out how
    to get the exception object when I don't specify a match.
    >
    for example:
    >
    >>>try: urlopen('http://www.google.com' )
    >>>except socket.error, msg:
    >>> print str(msg)
    >
    this works and I can do what I like with the exception object (msg).
    but I can't do this with a simple except statment.
    >
    >>>except msg:
    >
    this won't work because it will think msg is the type to match
    >
    >>>except ,msg:
    >
    syntax error
    >
    >>>except *,msg:
    >
    syntax error
    >
    >>>except (),msg:
    >
    Hmm I didn't try that one before I started this post. It doesn't give
    me a syntax error. I'll experiment.
    Even if the above syntax works, is this the way to do this? It seems
    sort of funky.
    >
    How do I do this? Thanks.
    pydoc sys.exc_info


    Diez

    Comment

    • John Machin

      #3
      Re: How can I obtain the exception object on a generlized except statement?

      On Jun 10, 10:13 pm, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
      Chris Allen schrieb:
      >
      >
      >
      I am confused on one aspect of exception handling. If you specify the
      exception object type to match in an except statement it is possible
      to also obtain the exception object itself, but I can't figure out how
      to get the exception object when I don't specify a match.
      >
      for example:
      >
      >>try: urlopen('http://www.google.com' )
      >>except socket.error, msg:
      >> print str(msg)
      >
      this works and I can do what I like with the exception object (msg).
      but I can't do this with a simple except statment.
      >
      >>except msg:
      >
      this won't work because it will think msg is the type to match
      >
      >>except ,msg:
      >
      syntax error
      >
      >>except *,msg:
      >
      syntax error
      >
      >>except (),msg:
      >
      Hmm I didn't try that one before I started this post. It doesn't give
      me a syntax error. I'll experiment.
      Even if the above syntax works, is this the way to do this? It seems
      sort of funky.
      >
      How do I do this? Thanks.
      >
      pydoc sys.exc_info
      >
      In particular, you want sys.exc_info()[:2]




      Comment

      • Chris Allen

        #4
        Re: How can I obtain the exception object on a generlized except statement?

        Just what I was looking for thanks Diez and John.

        Comment

        • Duncan Booth

          #5
          Re: How can I obtain the exception object on a generlized except statement?

          Chris Allen <ca.allen@gmail .comwrote:
          I am confused on one aspect of exception handling. If you specify the
          exception object type to match in an except statement it is possible
          to also obtain the exception object itself, but I can't figure out how
          to get the exception object when I don't specify a match.
          In most cases you can just catch Exception. If you want to be sure to
          catch deprecated string exceptions also then use sys.exc_info().

          try:
          ... something ...
          except Exception, e:
          print e

          Also what you want to do with it when you've caught it makes a
          difference:

          If you are planning on logging a stack backtrace then you'll want the
          traceback as well as the exception, so sys.exc_info() might be indicated
          except that in that case you'll be better off using logging.excepti on()
          instead.

          try:
          ... something ...
          except:
          logging.excepti on("Unexpected error")

          If you are just planning on swallowing the exception then you don't want
          any of these: you want to catch the specific exceptions that you expect
          to be raised.
          >except (),msg:
          Using an empty tuple for the exception specification won't catch any
          exceptions. Not very useful if the tuple is a literal, but it could be
          useful in some obscure situations (e.g. an except clause to handle
          exceptions declared in a specific DLL which might not always be
          present).

          Comment

          Working...