How do I get info on an exception ?

Frank Frank at home
Fri Jul 18 02:33:52 EDT 2003


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

>From IDLE, I can generate:
>>> socket.gethostbyaddr('1.2')
Traceback (most recent call last):
  File "<pyshell#28>", line 1, in ?
    socket.gethostbyaddr('1.2')
herror: (11004, 'host not found')   <=== what I want when I catch

When I run this code:
        try:
            hostname, aliases, hostip = socket.gethostbyaddr(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.herror 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




More information about the Python-list mailing list