Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89781 > unrolled thread

Try Except Specific Error Messages

Started by"brandon wallace" <nodnarb@gmx.us>
First post2015-05-01 01:27 +0200
Last post2015-05-01 01:27 +0200
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

  Try Except Specific Error Messages "brandon wallace" <nodnarb@gmx.us> - 2015-05-01 01:27 +0200

#89781 — Try Except Specific Error Messages

From"brandon wallace" <nodnarb@gmx.us>
Date2015-05-01 01:27 +0200
SubjectTry Except Specific Error Messages
Message-ID<mailman.18.1430585660.12865.python-list@python.org>
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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web