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


Groups > comp.lang.python > #89785

Re: Try Except Specific Error Messages

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gherron@digipen.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.058
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; 'messages.': 0.05; 'python3': 0.07; 'subject:Error': 0.07; 'try:': 0.09; 'way:': 0.09; 'err:': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'bit': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'errors.': 0.24; 'this:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'errors': 0.30; 'waste': 0.30; 'code': 0.31; 'gary': 0.31; 'text': 0.33; 'running': 0.33; 'skip:# 10': 0.33; 'actual': 0.34; 'could': 0.34; 'message.': 0.35; 'except': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'next': 0.36; 'useful': 0.36; 'hi,': 0.36; 'should': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'tell': 0.60; 'name': 0.63; 'more': 0.64; 'side': 0.67; 'webpage': 0.68; 'qualified': 0.72; 'institute': 0.72; 'received:204': 0.75; 'dr.': 0.77; 'cable': 0.84; 'wallace': 0.84
Date Sat, 02 May 2015 10:28:44 -0700
From Gary Herron <gherron@digipen.edu>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Try Except Specific Error Messages
References <trinity-d2e41874-219e-46a2-8768-44616fc2dce5-1430436471933@3capp-mailcom-lxa05>
In-Reply-To <trinity-d2e41874-219e-46a2-8768-44616fc2dce5-1430436471933@3capp-mailcom-lxa05>
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.22.1430587736.12865.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430587736 news.xs4all.nl 2939 [2001:888:2000:d::a6]:48641
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89785

Show key headers only | 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