title: handling exceptions from python requests
body: <p>My function is executed with threads:</p>
  
  <pre><code>def getdata(self, page, ...):
      tries = 10
      for n in range(tries):
          try:
              ...
              datarALL = []
              url = 'http://website/...'.format(...)
              responsedata = requests.get(url, data=data, headers=self.hed, verify=False)
              responsedata.raise_for_status()
              if responsedata.status_code == 200:  # 200 for successful call
                  ...
                      if ...
                          break   
          except (ChunkedEncodingError, requests.exceptions.HTTPError) as e:
              print ("page #{0} run #{1} failed. Returned status code {2}. Reason: {3}. Msg: {4}. Retry.".format(page, n, responsedata.status_code, responsedata.reason, sys.exc_info()[0]))
              if n == tries - 1:
                  print ("page {0} could not be imported. Max retried reached.".format(page))
                  os._exit(1)  #One thread max retried - close all threads and 
      return datarALL
  </code></pre>
  
  <p>as follows:</p>
  
  <pre><code>    with ThreadPoolExecutor(max_workers=num_of_workers) as executor:
          futh = [(executor.submit(self.getdata, page,...)) for page in pages]
          for data in as_completed(futh):
              datarALL.extend(data.result())
      print ("Finished generateing data.")
      return datarALL
  </code></pre>
  
  <p>Sometimes I get unexpected exceptions like: <code>ConnectionResetError: [Errno 104] Connection reset by peer</code> which shuts down my program. I want to change my code so that no matter which exception happens the thread will allways retry untill the <code>if n == tries - 1:</code> is met. I don't want my thread to shut down due to random exception.</p>
  
  <p>I read the <a href="http://docs.python-requests.org/en/master/_modules/requests/exceptions/" rel="nofollow noreferrer">request exceptions info page</a> but I don't see how I can catch all the exceptions without manually listing all of them. Is there a generic way to do this? </p>
  
  <p>Basicly I want something like:</p>
  
  <pre><code>        except (ALL EXCEPTIONS from Requests) as e:
              print ("page #{0} run #{1} failed. Returned status code {2}. Reason: {3}. Msg: {4}. Retry.".format(page, n, responsedata.status_code, responsedata.reason, sys.exc_info()[0]))
              if n == tries - 1:
                  print ("page {0} could not be imported. Max retried reached.".format(page))
                  os._exit(1)  #One thread max retried - close all threads and 
      return datarALL
  </code></pre>
  
  <p>How can I do that?</p>
  
  <p><strong>EDIT:</strong>
  using </p>
  
  <pre><code>    except Exception as e:
          print ("page #{0} run #{1} failed. Returned status code {2}. Reason: {3}. Msg: {4}. Retry.".format(page, n, responsedata.status_code, responsedata.reason, sys.exc_info()[0]))
          if n == tries - 1:
              print ("page {0} could not be imported. Max retried reached.".format(page))
              os._exit(1)  #One thread max retried - close all threads and 
  return datarALL
  </code></pre>
  
  <p>Does not catch. it gives me this:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/response.py", line 331, in _error_catcher
      yield
    File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/response.py", line 640, in read_chunked
      chunk = self._handle_chunk(amt)
    File "/home/ubuntu/.local/lib/python3.5/site-packages/urllib3/response.py", line 595, in _handle_chunk
      returned_chunk = self._fp._safe_read(self.chunk_left)
    File "/usr/lib/python3.5/http/client.py", line 607, in _safe_read
      chunk = self.fp.read(min(amt, MAXAMOUNT))
    File "/usr/lib/python3.5/socket.py", line 575, in readinto
      return self._sock.recv_into(b)
  ConnectionResetError: [Errno 104] Connection reset by peer
  </code></pre>
  
  <p>The loop does not retry. The run is terminated...</p>
  
  <p><strong>EDIT2:</strong></p>
  
  <pre><code>    except requests.exceptions.RequestException as e:
          print ("page #{0} run #{1} failed. Returned status code {2}. Reason: {3}. Msg: {4}. Retry.".format(page, n, responsedata.status_code, responsedata.reason, sys.exc_info()[0]))
          if n == tries - 1:
              print ("page {0} could not be imported. Max retried reached.".format(page))
              os._exit(1)  #One thread max retried - close all threads and 
  return datarALL
  </code></pre>
  
  <p>Also does not catch the <code>ConnectionResetError: [Errno 104] Connection reset by peer</code> listed above.</p>