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


Groups > comp.lang.python > #89785

Re: Try Except Specific Error Messages

Date 2015-05-02 10:28 -0700
From Gary Herron <gherron@digipen.edu>
Subject Re: Try Except Specific Error Messages
References <trinity-d2e41874-219e-46a2-8768-44616fc2dce5-1430436471933@3capp-mailcom-lxa05>
Newsgroups comp.lang.python
Message-ID <mailman.22.1430587736.12865.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Try Except Specific Error Messages Gary Herron <gherron@digipen.edu> - 2015-05-02 10:28 -0700

csiph-web