[docs] Issue in urllib2 doc for python 3.0 to 3.?

Adrix Remi electro.adrix at gmail.com
Mon Jun 23 18:21:51 CEST 2014


The issue is in the Handling Exeption
<https://docs.python.org/3.2/howto/urllib2.html#handling-exceptions> part
and more precisely in the last example
<https://docs.python.org/3.2/howto/urllib2.html#number-2>.

The order of the exception catching part is false:
It's like that in the doc :
------------------------------
from urllib.request import Request, urlopen
from urllib.error import  URLError
req = Request(someurl)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'reason'):
        print('We failed to reach a server.') # URLError handling
        print('Reason: ', e.reason)
    elif hasattr(e, 'code'):
        print('The server couldn\'t fulfill the request.') # HTTPError
handling
        print('Error code: ', e.code)
else:
    # everything is fine
------------------------------

and the correct order is :
------------------------------
from urllib.request import Request, urlopen
from urllib.error import  URLError
req = Request(someurl)
try:
    response = urlopen(req)
except URLError as e:
    if hasattr(e, 'code'):
        print('The server couldn\'t fulfill the request.') # HTTPError
handling
        print('Error code: ', e.code)
    elif hasattr(e, 'reason'):
        print('We failed to reach a server.') # URLError handling
        print('Reason: ', e.reason)
else:
    # everything is fine
------------------------------

Because, just as explained over the example (in the doc), if the URLError
handling comes first it will also catch the HTTPError.

I hope I'm clear enough

Have a nice day :-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20140623/637ca506/attachment.html>


More information about the docs mailing list