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


Groups > comp.lang.python > #89781

Try Except Specific Error Messages

From "brandon wallace" <nodnarb@gmx.us>
Subject Try Except Specific Error Messages
Date 2015-05-01 01:27 +0200
Newsgroups comp.lang.python
Message-ID <mailman.18.1430585660.12865.python-list@python.org> (permalink)

Show all headers | View raw


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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web