Tims' answers seem to me misleading. Especially when urllib2 does not return the expected code. For example, this error will be fatal (believe it or not - this is not uncommon when loading URLs):
AttributeError: a URLError object does not have a 'code' attribute
A quick, but perhaps not the best solution would be code using a nested try / except block:
import urllib2 try: urllib2.urlopen("some url") except urllib2.HTTPError, err: try: if err.code == 404:
Additional information on the topic of nested try / except blocks Are nested try / except blocks in python a good programming practice?
sonavolob May 19 '15 at 1:43 pm 2015-05-19 13:43
source share