error code

Alex Martelli aleax at aleax.it
Wed Jul 24 12:03:17 EDT 2002


ferreira jorge wrote:

> In your example you write the Exception code .
> 
> for example :
> try :
>     server = smtplib.SMTP('XXXXXXX')
> except :
>     print "error : %s --> %s " % ( sys.exc_value , sys.exc_type)
> 
> If I put a bad host , I have the error : "error : host not found -->
> socket.error"
> 
> I suppose that the error "host not found" have a caracteristic number .
> how can I get it ?

Don't use except without arguments, it's almost never best.

>>> import smtplib, socket
>>> try: server=smtplib.SMTP('XXXXXX')
... except socket.error, err: pass
...
>>> err
<socket.gaierror instance at 0x81a75f4>
>>> dir(err)
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
>>> err.args
(-2, 'Name or service not known')
>>>

In lieu of the 'pass' put any processing you may desire on
err.args.


Alex




More information about the Python-list mailing list