0

I've been reading through http://www.mobify.com/blog/http-requests-are-hard/ , which discusses various types of errors which can be encountered with a request. The article focuses on catching each one. I would like to simply print out the error type whenever any error occurs. In the article , one example is:

url = "http://www.definitivelydoesnotexist.com/"

try:
    response = request.get(url)
except requests.exceptions.ConnectionError as e:
    print "These aren't the domains we're looking for."

Is there a way to rewrite the last 2 lines in pseudocode as:

except requests.ANYERROR as e:
    print e
user1592380
  • 30,233
  • 76
  • 247
  • 468

2 Answers2

3

From my other answer:

All exceptions that Requests explicitly raises inherit from requests.exceptions.RequestException.

So this should catch everything:

try:
    response = requests.get(url)
except requests.exceptions.RequestException as e:
    print e
Community
  • 1
  • 1
Jonathon Reinhart
  • 124,861
  • 31
  • 240
  • 314
1

All of the requests exceptions inherit from requests.exceptions.RequestException, so you can:

try:
    ....
    ....
except requests.exceptions.RequestException as e:
    # Do whatever you want to e
    pass
Dekel
  • 57,326
  • 8
  • 92
  • 123