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
  • 34,265
  • 92
  • 284
  • 515

2 Answers2

4

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
  • 132,704
  • 33
  • 254
  • 328
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
  • 60,707
  • 10
  • 101
  • 129