How do I get info on an exception ?

Ian Bicking ianb at colorstudy.com
Fri Jul 18 02:39:39 EDT 2003


On Fri, 2003-07-18 at 01:33, Frank wrote:
> 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

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







More information about the Python-list mailing list