How do I get info on an exception ?

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

    How do I get info on an exception ?

    Using Python 2.2.2,
    I want to catch all exceptions from "socket.gethost byaddr(ip)"

    From IDLE, I can generate:[color=blue][color=green][color=darkred]
    >>> socket.gethostb yaddr('1.2')[/color][/color][/color]
    Traceback (most recent call last):
    File "<pyshell#2 8>", line 1, in ?
    socket.gethostb yaddr('1.2')
    herror: (11004, 'host not found') <=== what I want when I catch

    When I run this code:
    try:
    hostname, aliases, hostip = socket.gethostb yaddr(ip)
    return (hostip, hostname)
    except:
    print sys.exc_info()
    print sys.exc_type
    return

    The "print sys.exc_info()" gives-
    (<class socket.herror at 0x00AE7AC0>,
    <socket.herro r instance at 0x00C725C0>,
    <traceback object at 0x00C73920>)

    And the "print sys.exc_type" gives-
    socket.herror

    How do I get the "(11004, 'host not found')" part?
    More importantly, where is the answer documented that I should
    have looked?

    Frank
  • Erik Max Francis

    #2
    Re: How do I get info on an exception ?

    Frank wrote:
    [color=blue]
    > Using Python 2.2.2,
    > I want to catch all exceptions from "socket.gethost byaddr(ip)"
    >
    > From IDLE, I can generate:[color=green][color=darkred]
    > >>> socket.gethostb yaddr('1.2')[/color][/color]
    > Traceback (most recent call last):
    > File "<pyshell#2 8>", line 1, in ?
    > socket.gethostb yaddr('1.2')
    > herror: (11004, 'host not found') <=== what I want when I catch
    >
    > When I run this code:
    > try:
    > hostname, aliases, hostip = socket.gethostb yaddr(ip)
    > return (hostip, hostname)
    > except:
    > print sys.exc_info()
    > print sys.exc_type
    > return[/color]

    Use the format:

    try:
    ...
    except ErrorType, e:
    ... do something with exception object e ...
    [color=blue][color=green][color=darkred]
    >>> import socket
    >>> socket.error[/color][/color][/color]
    <class socket.error at 0x8154304>[color=blue][color=green][color=darkred]
    >>> try:[/color][/color][/color]
    .... socket.gethostb yaddr('1.2')
    .... except socket.error, e:
    .... print e, dir(e), e.args
    ....
    (1, 'Unknown host') ['__doc__', '__getitem__', '__init__', '__module__',
    '__str__', 'args'] (1, 'Unknown host')
    [color=blue]
    > How do I get the "(11004, 'host not found')" part?
    > More importantly, where is the answer documented that I should
    > have looked?[/color]

    Check the part on exception handling.

    --
    Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ It is human nature to think wisely and act foolishly.
    \__/ Anatole France

    Comment

    • Heiko Wundram

      #3
      Re: How do I get info on an exception ?

      > How do I get the "(11004, 'host not found')" part?[color=blue]
      > More importantly, where is the answer documented that I should
      > have looked?[/color]

      Look at the traceback module from the Python standard library. If you
      just wish to print the exception, do the following:

      import traceback

      try:
      nosuchobject
      except:
      traceback.print _exc()

      HTH!

      Heiko.


      Comment

      • Ian Bicking

        #4
        Re: How do I get info on an exception ?

        On Fri, 2003-07-18 at 01:33, Frank wrote:[color=blue]
        > Using Python 2.2.2,
        > I want to catch all exceptions from "socket.gethost byaddr(ip)"
        >[color=green]
        > >From IDLE, I can generate:[color=darkred]
        > >>> socket.gethostb yaddr('1.2')[/color][/color]
        > Traceback (most recent call last):
        > File "<pyshell#2 8>", line 1, in ?
        > socket.gethostb yaddr('1.2')
        > herror: (11004, 'host not found') <=== what I want when I catch[/color]
        [color=blue]
        > When I run this code:
        > try:
        > hostname, aliases, hostip = socket.gethostb yaddr(ip)
        > return (hostip, hostname)
        > except:
        > print sys.exc_info()
        > print sys.exc_type
        > return[/color]

        try:
        do exceptional stuff...
        except Exception, e:
        print e.args, dir(e)

        Probably that information is just in e.args, but you can add other
        attributes to exceptions, any of which should be revealed by dir(e).

        Exceptions are poorly documented, so I don't know where that might be
        noted.

        Ian



        Comment

        • Bengt Richter

          #5
          Re: How do I get info on an exception ?

          On Thu, 17 Jul 2003 23:29:40 -0700, Erik Max Francis <max@alcyone.co m> wrote:
          [color=blue]
          >Frank wrote:
          >[color=green]
          >> Using Python 2.2.2,
          >> I want to catch all exceptions from "socket.gethost byaddr(ip)"
          >>
          >> From IDLE, I can generate:[color=darkred]
          >> >>> socket.gethostb yaddr('1.2')[/color]
          >> Traceback (most recent call last):
          >> File "<pyshell#2 8>", line 1, in ?
          >> socket.gethostb yaddr('1.2')
          >> herror: (11004, 'host not found') <=== what I want when I catch
          >>
          >> When I run this code:
          >> try:
          >> hostname, aliases, hostip = socket.gethostb yaddr(ip)
          >> return (hostip, hostname)
          >> except:
          >> print sys.exc_info()
          >> print sys.exc_type
          >> return[/color]
          >
          >Use the format:
          >
          > try:
          > ...
          > except ErrorType, e:
          > ... do something with exception object e ...
          >[/color]
          Indeed. Or if you want to catch all standard exceptions until you know what is
          happening, you could pick from the following. I guess you could also catch all
          exceptions defined in a module without knowing what they're named, and re-raise
          if other, e.g., with (untested!)

          if not repr(e.__class_ _).startswith(' socket'): raise

          is there another way to do that?
          [color=blue][color=green][color=darkred]
          >>> import socket
          >>> try:[/color][/color][/color]
          ... socket.gethostb yaddr('1.2')
          ... except Exception, e:
          .... print 'plain e:', e
          .... print 'e class:', e.__class__
          .... print 'e class name:', e.__class__.__n ame__
          .... print 'vars keys:', vars(e).keys()
          .... print 'vars dict:', vars(e)
          .... print 'additional inherited clutter seen by dir:\n',dir(e)
          ....
          plain e: (11004, 'host not found')
          e class: socket.herror
          e class name: herror
          vars keys: ['args']
          vars dict: {'args': (11004, 'host not found')}
          additional inherited clutter seen by dir:
          ['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']

          You could also print a formatted vars(e) to see unanticipated attributes more nicely
          with e.g., (untested!) print ''.join(['%16s = %r\n' %(k,v) for k,v in vars(e).items()])
          [color=blue][color=green][color=darkred]
          >>>> import socket
          >>>> socket.error[/color][/color]
          ><class socket.error at 0x8154304>[color=green][color=darkred]
          >>>> try:[/color][/color]
          >... socket.gethostb yaddr('1.2')
          >... except socket.error, e:
          >... print e, dir(e), e.args
          >...
          >(1, 'Unknown host') ['__doc__', '__getitem__', '__init__', '__module__',
          >'__str__', 'args'] (1, 'Unknown host')
          >[color=green]
          >> How do I get the "(11004, 'host not found')" part?
          >> More importantly, where is the answer documented that I should
          >> have looked?[/color]
          >
          >Check the part on exception handling.
          >[/color]
          Interactively, help('exception s') is also helpful (note quotes)

          Regards,
          Bengt Richter

          Comment

          • Cameron Laird

            #6
            Re: How do I get info on an exception ?

            In article <U2NRa.7475$7O. 275@nwrdny01.gn ilink.net>,
            Raymond Hettinger <python@rcn.com > wrote:

            Comment

            • Thomas Heller

              #7
              Re: How do I get info on an exception ?

              >>> >The list of possible socket exceptions is in the docs for sockets.[color=blue][color=green][color=darkred]
              >>> >It also describes the (errno, string) return tuple value of inst.args.
              >>> .
              >>> True.
              >>>
              >>> But unsatisfying--at least to me.[/color]
              >>
              >>Submit a patch.[/color]
              > .
              > .
              > I deserved that.
              >
              > There was more to my post, of course. Part of what I was trying to
              > express is that exception interfaces are almost always relegated to
              > a footnote, with only a generalized description, even in the frequent
              > case that a method's exceptions are more complicated than its invoca-
              > tions.
              >
              > Rather than tilt at the collection of all such windmills, I want to
              > first understand better why this is, and what response is appropriate.
              > To summarize: I don't know what patch is the right one. I also
              > thought it only fair to warn Mr. July that things are indeed more dif-
              > ficult than we were explaining, even though I didn't feel up to
              > detailing the difficulties.
              >
              > So, Raymond, do you have general guidelines for how you think excep-
              > tions should be documented?[/color]

              I don't know what Raymond will suggest, but for myself I always (well,
              nearly always) when I find the docs insufficient, I'll submit a patch
              which contains what I would like to read there.
              Works pretty well so far...

              Thomas

              Comment

              • Raymond Hettinger

                #8
                Re: How do I get info on an exception ?

                [Cameron Laird][color=blue][color=green][color=darkred]
                > >> But unsatisfying--at least to me.[/color]
                > >
                > >Submit a patch.[/color]
                > .
                > .
                > .
                > I deserved that.[/color]

                It wasn't a jab.
                Truly, if you see a way to clarify the docs, submit a patch.
                If everyone finds it helpful, it will be accepted. That's how
                the docs improve over time. I've personally written or
                accepted over a hundred of these in the past year.

                [color=blue]
                > There was more to my post, of course. Part of what I was trying to
                > express is that exception interfaces are almost always relegated to
                > a footnote, with only a generalized description, even in the frequent
                > case that a method's exceptions are more complicated than its invoca-
                > tions.[/color]

                No doubt, exceptions often get less explanation than everything else.
                However, in this case, the docs seemed reasonably clear to me.

                The part that could be improved is where it talks about returning a
                message or a tuple, as if either one but not both could occur at any
                time. It would be more accurate to say, that in all cases an exception
                instance is returned; the instance has a __str__ method so it can be
                printed as if it were a string; the instance has an attribute "args" which
                is the tuple (errno, errmsg); and the instance has a __getitem__ method
                so the args tuple can be directly unpacked as if the instance had been
                the tuple itself.

                OTOH, that is long-winded and distracts from the flow of knowledge
                about sockets. It is also how all exception instances work.

                [color=blue]
                > So, Raymond, do you have general guidelines for how you think excep-
                > tions should be documented?[/color]

                Unless there are interesting values being returned, it is probably
                sufficient to name the exception, state what it represents, and
                describe where it fits in the class structure (i.e. socket.herror is a
                subclass of socket.error). When you think about it, why have
                three paragraphs on an exception whose code is as simple as:

                class xyzException(Ex ception): pass # raised when xyz occurs

                When interesting values are returned, describe the meaning of
                each element in the instance's arg tuple (i.e. (errno, errmsg)).
                Afterall, the code behind it may be as simple as:

                raise socket.herror(e rrno, errmsg) # reveal the packet's error information

                Elsewhere in the docs, functions descriptions should list the names
                of the exceptions they can raise unless it is obvious or pertains
                to a whole collection of functions (i.e. all high-volume socket
                operations can potentially raise a timeout).

                In general, I like the documents to be as specific as possible without
                replicating the code or locking in a particular implementation. However,
                there is a competing force to keep module documents centered on how
                to use the module. For example, I found the unittest documentation to
                be highly specific and thorough, yet unsatisfactory because you practically
                had to read a book on the subject to be able to make basic use of the
                module. To fix it, I added a new introductory section that covers
                (in one page) the 5% of the module that will meet 95% of your needs
                (enough to get you up and running).


                Raymond Hettinger


                Comment

                • Frank

                  #9
                  Re: How do I get info on an exception ?

                  On Tue, 22 Jul 2003 17:59:48 GMT, "Raymond Hettinger"
                  <vze4rx4y@veriz on.net> wrote:
                  [color=blue]
                  >[Cameron Laird][color=green][color=darkred]
                  >> >> But unsatisfying--at least to me.
                  >> >
                  >> >Submit a patch.[/color]
                  >> .
                  >> .
                  >> .
                  >> I deserved that.[/color]
                  >
                  >It wasn't a jab.
                  >Truly, if you see a way to clarify the docs, submit a patch.
                  >If everyone finds it helpful, it will be accepted. That's how
                  >the docs improve over time. I've personally written or
                  >accepted over a hundred of these in the past year.
                  >
                  >[color=green]
                  >> There was more to my post, of course. Part of what I was trying to
                  >> express is that exception interfaces are almost always relegated to
                  >> a footnote, with only a generalized description, even in the frequent
                  >> case that a method's exceptions are more complicated than its invoca-
                  >> tions.[/color]
                  >
                  >No doubt, exceptions often get less explanation than everything else.
                  >However, in this case, the docs seemed reasonably clear to me.
                  >
                  >The part that could be improved is where it talks about returning a
                  >message or a tuple, as if either one but not both could occur at any
                  >time. It would be more accurate to say, that in all cases an exception
                  >instance is returned; the instance has a __str__ method so it can be
                  >printed as if it were a string; the instance has an attribute "args" which
                  >is the tuple (errno, errmsg); and the instance has a __getitem__ method
                  >so the args tuple can be directly unpacked as if the instance had been
                  >the tuple itself.
                  >
                  >OTOH, that is long-winded and distracts from the flow of knowledge
                  >about sockets. It is also how all exception instances work.
                  >
                  >[color=green]
                  >> So, Raymond, do you have general guidelines for how you think excep-
                  >> tions should be documented?[/color]
                  >
                  >Unless there are interesting values being returned, it is probably
                  >sufficient to name the exception, state what it represents, and
                  >describe where it fits in the class structure (i.e. socket.herror is a
                  >subclass of socket.error). When you think about it, why have
                  >three paragraphs on an exception whose code is as simple as:
                  >
                  > class xyzException(Ex ception): pass # raised when xyz occurs
                  >
                  >When interesting values are returned, describe the meaning of
                  >each element in the instance's arg tuple (i.e. (errno, errmsg)).
                  >Afterall, the code behind it may be as simple as:
                  >
                  > raise socket.herror(e rrno, errmsg) # reveal the packet's error information
                  >
                  >Elsewhere in the docs, functions descriptions should list the names
                  >of the exceptions they can raise unless it is obvious or pertains
                  >to a whole collection of functions (i.e. all high-volume socket
                  >operations can potentially raise a timeout).
                  >
                  >In general, I like the documents to be as specific as possible without
                  >replicating the code or locking in a particular implementation. However,
                  >there is a competing force to keep module documents centered on how
                  >to use the module. For example, I found the unittest documentation to
                  >be highly specific and thorough, yet unsatisfactory because you practically
                  >had to read a book on the subject to be able to make basic use of the
                  >module. To fix it, I added a new introductory section that covers
                  >(in one page) the 5% of the module that will meet 95% of your needs
                  >(enough to get you up and running).
                  >
                  >
                  >Raymond Hettinger
                  >[/color]
                  Thanks to all for your help.

                  After checking that the input is a string, I try calling
                  gethostbyname/addr depending on whether the first char is numeric and
                  catch:
                  "except socket.error, err:"
                  Will this catch all possible errors? I've tried feeding various
                  errors into my def and caught them all so far. Input might come from
                  users at the keyboard, and crashing the program is not an option, no
                  matter what is typed in.
                  ------------------------------------
                  I did look for the solution before posting; I checked:
                  Python Lib Ref 2.2.2
                  Python Language Ref 2.2.2
                  Lutz - Learning Python
                  Lutz - Programming Python, 2nd
                  Holden - Python Web Programming
                  Brueck - Python 2.1 Bible
                  Martelli - Python in a Nutshell
                  and even Guido's Tutorial. If the complete answer is there, I could
                  not find it or assemble all the parts from the various references.
                  Perhaps with more experience in Python it would have been clearer, but
                  I have to agree with those that say exceptions are not well
                  documented. Complete and precise documentation is probably more
                  important for a language/compiler/interpreter system than any anything
                  else.

                  Frank

                  Comment

                  • Bengt Richter

                    #10
                    Re: How do I get info on an exception ?

                    On Tue, 22 Jul 2003 16:44:04 -0000, claird@lairds.c om (Cameron Laird) wrote:
                    [color=blue]
                    >Raymond Hettinger <python@rcn.com > wrote:[color=green][color=darkred]
                    >>> >You could catch it with:
                    >>> >
                    >>> > except socket.herror, inst:
                    >>> > print inst.args
                    >>> >
                    >>> >or more broadly with:
                    >>> >
                    >>> > except socket.error, (errno, string_message) :
                    >>> > print code, message
                    >>> >
                    >>> >
                    >>> >> More importantly, where is the answer documented that I should
                    >>> >> have looked?
                    >>> >
                    >>> >The list of possible socket exceptions is in the docs for sockets.
                    >>> >It also describes the (errno, string) return tuple value of inst.args.
                    >>> .
                    >>> .
                    >>> .
                    >>> True.
                    >>>
                    >>> But unsatisfying--at least to me.[/color]
                    >>
                    >>Submit a patch.[/color]
                    > .
                    > .
                    > .
                    >I deserved that.
                    >
                    >There was more to my post, of course. Part of what I was trying to
                    >express is that exception interfaces are almost always relegated to
                    >a footnote, with only a generalized description, even in the frequent
                    >case that a method's exceptions are more complicated than its invoca-
                    >tions.
                    >
                    >Rather than tilt at the collection of all such windmills, I want to
                    >first understand better why this is, and what response is appropriate.
                    >To summarize: I don't know what patch is the right one. I also
                    >thought it only fair to warn Mr. July that things are indeed more dif-
                    >ficult than we were explaining, even though I didn't feel up to
                    >detailing the difficulties.
                    >
                    >So, Raymond, do you have general guidelines for how you think excep-
                    >tions should be documented?[/color]

                    ISTM there are (at least) two important places for doc info: Code-associated and
                    language/tool-usage-associated. There are docs we approach as docs like books to read,
                    and there are embedded docs as in doc strings and comments. And pydoc sort of
                    bridges the gap and makes a book from code and whatall.

                    I wonder where the best place is to enhance the documentation accessible to users.
                    I suspect it would be in the area of pydoc and the structure of "code and whatall."
                    The most likely place to find up-to-date info is in the latest code. (See below
                    for an idea of how to enhance the latest code doc info without changing known-good
                    code source).

                    Exceptions (subject topic ;-) are a special case. In principle any code documentation
                    could be enhanced by adopting source conventions to help pydoc present better info.
                    The question then becomes how best to format the doc-enhancing info, where to put it,
                    and how best to support its presentation.

                    One thing that is a deterrent to improved documentation *within* code is that people
                    like to minimize the number of official code versions, whether by CVS or just noting
                    MD5 digests etc., and/or by burning an "official" CDROM. This makes me wonder if it would
                    make sense to have a way to store doc info separately, yet still integratable into a pydoc
                    presentation.

                    One way would be to have a file name convention, so that the optional doc-enhancement
                    info for e.g., xxx.py would be found in, e.g., xxx.dpy. This could make a clutter of small
                    files though, so maybe one or more .dpx files could contain the mapping from multiple
                    small related sources to a single .dpy. And by convention .dpy info for packaged files
                    would be looked for in a .dpy for the overall package if not found separately.

                    Ok, we could hone this search mechanism. What about the structure of .dpy files themselves,
                    which now potentially could have info enhancing docs for both xxx.py and yyy.py?

                    Some kind of easy internal labeled sections to separate xxx.py from yyy.py info
                    would seem obvious. Then, the real question: what goes in the .dpy section for xxx.py
                    so as to tie into certain parts of xxx.py code, without requiring changes to that code
                    (which, remember, may be on a CDROM etc)?

                    The context-finding mechanism of diff/patch would be one option, but to use that literally
                    would make the .dpy files into diff files, and probably not something easy to generate
                    manually and readably. And you don't want to carry a whole source fork just to maintain
                    doc enhancement .dpy files. So we need some conventions functionally to enable human-doable
                    doc diff patches. Maybe with special easy syntax to refer to exception raising loci
                    in the code vs catching, vs functions, classes, methods, existing doc strings, etc, etc.

                    An advantage of pure doc-enhancement info is that a mistake can't break the actual code,
                    so more people could be given commit privileges for that kind of files, if maintained
                    separately on CVS. Even wide-open wiki methods could be considered. You could even configure
                    pydoc to query python.org efficiently to check if a local cache is out of date, and prompt
                    for optional download.

                    But this is a matter of arriving at some PEP consensus after discussing 1) whether it's
                    worth discussing, and 2) what the usability goals are for both users and creators of
                    doc enhancement info, and 3) who has the time to follow up on stuff like this.
                    Just writing this post cost something, as will your replies (if any ;-)

                    Regards,
                    Bengt Richter

                    Comment

                    • Raymond Hettinger

                      #11
                      Re: How do I get info on an exception ?

                      [Raymond][color=blue][color=green]
                      > >The part that could be improved is where it talks about returning a
                      > >message or a tuple, as if either one but not both could occur at any
                      > >time. It would be more accurate to say, that in all cases an exception
                      > >instance is returned; the instance has a __str__ method so it can be
                      > >printed as if it were a string; the instance has an attribute "args" which
                      > >is the tuple (errno, errmsg); and the instance has a __getitem__ method
                      > >so the args tuple can be directly unpacked as if the instance had been
                      > >the tuple itself.[/color][/color]

                      [Frank][color=blue]
                      > After checking that the input is a string, I try calling
                      > gethostbyname/addr depending on whether the first char is numeric and
                      > catch:
                      > "except socket.error, err:"
                      > Will this catch all possible errors? I've tried feeding various
                      > errors into my def and caught them all so far. Input might come from
                      > users at the keyboard, and crashing the program is not an option, no
                      > matter what is typed in.[/color]

                      Yes, that will catch all errors. You have bound the instance to "err".

                      As I said earlier, the part of the docs that some folks may
                      find confusing is that part about returning a string or a tuple.
                      In fact, it always returns an instance that can be printed like
                      a string *and* unpacked like a tuple.

                      I'll modify the docs to make this more clear.

                      If you're in an experimental frame of mind, it is straight-forward to
                      learn exactly what is going on by adding a few print statements:

                      except socket.error, inst:
                      print 'Caught a', type(inst) # show that an exception instance was
                      recd
                      print 'The inst can be printed like a string:', inst
                      errno, errmsg = inst # show the exception can be unpacked
                      print 'It contains a tuple with', errno, 'and', errmsg
                      print 'Its methods are', dir(inst)
                      print 'The data is kept in inst.args', inst.args
                      print 'It prints because of the __str__ method:', inst.__str__()
                      print 'That is my story and I am sticking with it!'


                      Raymond Hettinger




                      Raymond Hettinger


                      Comment

                      Working...