Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89785 > unrolled thread
| Started by | Gary Herron <gherron@digipen.edu> |
|---|---|
| First post | 2015-05-02 10:28 -0700 |
| Last post | 2015-05-02 10:28 -0700 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Try Except Specific Error Messages Gary Herron <gherron@digipen.edu> - 2015-05-02 10:28 -0700
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2015-05-02 10:28 -0700 |
| Subject | Re: Try Except Specific Error Messages |
| Message-ID | <mailman.22.1430587736.12865.python-list@python.org> |
On 04/30/2015 04:27 PM, brandon wallace wrote:
> 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>
Note the error message here. The exception is named
"urllib.error.URLError" NOT "URLError" as you use in the next bit of code.
You could import that specific error if you wanted to access it that way:
from urllib.error import URLError
otherwise you should use the fully qualified name urllib.error.URLError.
>
> I tried this but got more error messages.
As a side note, that is never a useful thing to say in this group. Take
the time to tell is the actual errors message you got. That way I don't
have to waste my time running your code to see what error message you
are getting.
> 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?
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
Back to top | Article view | comp.lang.python
csiph-web