5

I'm doing this:

import requests r = requests.get("http://non-existent-domain.test") 

And getting

ConnectionError: HTTPConnectionPool(host='non-existent-domain.test', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x10b0170f0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',)) 

However, if I try to catch it like this:

try: r = requests.get("http://non-existent-domain.test") except ConnectionError: print("ConnectionError") 

Nothing changes, I still have ConnectionError unhandled. How to catch it properly?

1 Answer 1

7

That's a different ConnectionError. You are catching the built-in one, but requests has its own. So this should be

try: r = requests.get("http://non-existent-domain.test") except requests.ConnectionError: print("ConnectionError") # Output: ConnectionError 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.