title: Python: Continue looping after exception
body: <p>I have the following script (below). which will return the status code of URLs. It loops through a file and tries to connect to each host. Only problem is that it obviously stops looping when it reaches an exception.</p>
  
  <p>I have tried numerous things to put the how of it in a loop, but to no avail. Any thoughts?</p>
  
  <pre><code>import urllib
  import sys
  import time
  
  hostsFile = "webHosts.txt"
  
  
  try:
      f = file(hostsFile)
      while True:
          line = f.readline().strip()
          epoch = time.time()
          epoch = str(epoch)
          if len(line) == 0:
              break
          conn = urllib.urlopen(line)
          print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
  except IOError:
      epoch = time.time()
      epoch = str(epoch)
      print epoch + ": Connection unsuccessful, unable to connect to server, potential routing issues\n"
      sys.exit()
  else:
      f.close()
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I've come up with this in the mean-time, any issues with this? (i'm still learning :p )...</p>
  
  <pre><code>f = file(hostsFile)
  while True:
      line = f.readline().strip()
      epoch = time.time()
      epoch = str(epoch)
      if len(line) == 0:
          break
      try:
          conn = urllib.urlopen(line)
          print epoch + ": Connection successful, status code for " + line + " is " + str(conn.code) + "\n"
      except IOError:
          print epoch + "connection unsuccessful"
  </code></pre>
  
  <p>Thanks,</p>
  
  <p>MHibbin</p>