[Tutor] Exception Handling

Cameron Simpson cs at zip.com.au
Sat Oct 3 05:49:13 CEST 2015


On 03Oct2015 00:51, ALAN GAULD <alan.gauld at btinternet.com> wrote:
>On 02/10/15 23:57, Nym City via Tutor wrote:
>>socket.gaierror: [Errno 11004] getaddrinfo failed
>...
>>for name in ListOfHostNames:
>>     try:
>>         ResolveHostname = socket.gethostbyname(name)
>>         print(ResolveHostname)
>>         newFile.write(ResolveHostname + "\n")
>>         print(ResolveHostname)
>>     except socket.herror as e:
>>         newFile.write("No resolution available for %s" % (name) + "\n")
>
>You are catching herror but your code is resulting in gaierror.
>
>Add socket.gaierror to your except line.
>
>    except (socket.herror, socket.gaierror):
>        newFile.write("No resolution available for %s" % (name) + "\n")
>
>see if that works

Just a followon remark: always try to print the exception when you're not 
certain of what it will be or what to do with it. So I'd augument Alan's code 
like this:

     except (socket.herror, socket.gaierror) as e:
         newFile.write("No resolution available for %s: %s" % (name, e) + "\n")

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list