Reputation: 268
The title of the question may be a bit confusing but I don't really know how best to word it... I've found the following chunk of code which downloads a web page from the web by making use of the urllib2 library.
import urllib2
def download(url):
try:
html = urllib2.urlopen(url).read()
except urllib2.URLError as e:
print 'Download error:', e.reason
html = None
return html
Now if it happens that e.code
is 404 then e.reason
is simply an empty string which means it bears absolutely no information on what triggered the error, thus I don't really understand the point of using e.reason
here.
It seems like it would be more reasonable to print e
instead but even if I change it to simply print e
it will still yield something awkward: HTTP Error 404:
and the colon is apparenty followed by an empty string...
So it appears to me that the abovementioned code is a little clumsy in terms of exception handling. Is it so?
Upvotes: 1
Views: 443
Reputation: 186
It would seem that you could either use the error itself (print e) or the code and the reason (print "Download Error: ", e.code, e.reason) if you wanted to see the 404 code.
Upvotes: 1