Python Forum
Error handling not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Error handling not working (/thread-3696.html)



Error handling not working - desudesu - Jun-14-2017

Hi, i made a program that reads a URL from textfile and checks if that websites status is <=400, if it is, it will return "website is running" and saves that to a list and checks the next address in the textfile. This works as long as the website is reachable and returns status <= 400.
But when i add a random address www.asdasdasda123sd.com, which doesnt exist the program stops running completely. Need help solving this, code below and error also, thank you.

def getApacheStatus(urlStatus):
   r = requests.head(urlStatus['url'])
   try:
       if r.status_code <= 400:
           return 'connected'
       else:
           return 'website down'
   except:
       pass
Error:
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.asdasdasda123sd.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x7f8424b7de10>: Failed to establish a new connection: [Errno -2] Name or service not known',))
EDIT by user:
never mind, im stupid. found my mistake as soon as i posted problem, can delete this :D


RE: Error handling not working - buran - Jun-14-2017

r = requests.head(urlStatus['url'])
is out of the try/except block, thus making it pointless.


RE: Error handling not working - desudesu - Jun-14-2017

Yes, noticed that soon as i hit "post" :) been watching too much code, stupid mistakes