Tutorial :Check if command had some trouble in Python?



Question:

I have this command

h = urllib.urlopen("http://google.com/")  

How can I check if it retrieves me some error, internet down or something I don't expect?

For example, something like

if error print 'ERROR'

Thank you


Solution:1

urllib.urlopen is deprecated as it was removed in Python 3.0. Use urllib2.urlopen instead and in the documentation it says:

Raises URLError on errors.

So do:

try:      h = urllib2.urlopen("http://google.com/")  except urllib2.URLError as e:      # do something with the exception e.g.      print 'Error:', e.reason  

or just a (meaningless) error message:

try:      h = urllib2.urlopen("http://google.com/")  except urllib2.URLError:      print 'An error occurred.'  

URLError:

exception urllib2.URLError

The handlers raise this exception (or derived exceptions) when they run into a problem. It is a subclass of IOError.

reason
The reason for this error. It can be a message string or another exception instance (socket.error for remote URLs, OSError for local URLs).


You might want to read about Errors and Exceptions.


Solution:2

  • If that isn't able to do something that makes sense, it will raise an exception, which you can accept with try/except.

  • urllib.urlopen is deprecated in favour of urllib2.urlopen.

    >>> import urllib2  >>> urllib2.urlopen('not a valid URL')  Traceback (most recent call last):    File "<stdin>", line 1, in <module>      ...  ValueError: unknown url type: not a valid URL  >>>  >>> urllib2.urlopen("http://thiswebsitedoesntexistq.com")  Traceback (most recent call last):    File "<stdin>", line 1, in <module>      ...  urllib2.HTTPError: HTTP Error 404: Not Found  >>>  >>> # URLError is the parent class for the errors raised by urlopen   ... # if it can parse the URL  ...  >>> issubclass(urllib2.HTTPError, urllib2.URLError)   True  

I could catch these like

try:      urllib2.urlopen(some_url)  except ValueError:      print "Not a real URL"  except urllib2.HTTPError:      print "%s does not exist" % (some_url,)  

or I could let them propagate if I didn't have some sane way to react. (Oftentimes people want to catch an error so they can print the error and close their program. This is usually silly, since that's what Python would do anyway if you didn't catch and fix the error.)


Solution:3

An exception such as an IOError might occur - you could use exception handling to deal with this. For example:

try:      h = urllib.urlopen("http://google.com/")  except IOError as e:                           print "Error opening URL"  


Solution:4

I believe an exception is thrown on error. If you catch that and use the error code you can easilly do the appropriate error handling.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »