Python Forum

Full Version: Error handling not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
r = requests.head(urlStatus['url'])
is out of the try/except block, thus making it pointless.
Yes, noticed that soon as i hit "post" :) been watching too much code, stupid mistakes