Try Except Specific Error Messages
brandon wallace
nodnarb at gmx.us
Thu Apr 30 19:27:52 EDT 2015
Hi,
I am try to get more specific error messages using try/except.
I ran this code with the cable unplugged to see the error message. I got
#!/usr/bin/env python3
import urllib.request
webpage = urllib.request.urlopen("http://fakewebsite.com/")
text = webpage.read().decode("utf8")
I got two errors. This:
[....]
socket.gaierror: [Errno -2] Name or service not known
and this:
[....]
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>
I tried this but got more error messages.
try:
webpage = urllib.request.urlopen("http://fakewebsite.com/")
text = webpage.read().decode("utf8")
except URLError as err:
print("URLError: " + str(err))
How do I wrap urllib.request with try/except?
More information about the Python-list
mailing list