title: How to catch the error when I send XML to an invalid URL? (Python)
body: <p>I have a simple function that sends an XML string to a URL and takes a response. I am not sure how to handle the case where the URL is invalid. I've tried to put "URLError" and "socket.gaierror" (both of which show up in the error output), but neither seems to catch the invalid URL Error.</p>
  
  <pre><code>import urllib.request as request
  
  def getResponse(&lt;params&gt;):
      xmlReq = "an XML string"
  
      #make a new request
      new_req = request.Request(url="&lt;an invalid URL&gt;", 
                data=xmlReq)        
  
      try:
          xml_response = request.urlopen(new_req)
      except &lt;not sure what error goes here to catch invalid URL&gt;:
          print("Invalid URL. Aborting process.")
          quit()
  </code></pre>
  
  <p>This is the raw error output without the try-except block:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 1318, in do_open
      encode_chunked=req.has_header('Transfer-encoding'))
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 1239, in request
      self._send_request(method, url, body, headers, encode_chunked)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 1285, in _send_request
      self.endheaders(body, encode_chunked=encode_chunked)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 1234, in endheaders
      self._send_output(message_body, encode_chunked=encode_chunked)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 1026, in _send_output
      self.send(msg)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 964, in send
      self.connect()
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 1392, in connect
      super().connect()
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\http\client.py", line 936, in connect
      (self.host,self.port), self.timeout, self.source_address)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\socket.py", line 704, in create_connection
      for res in getaddrinfo(host, port, 0, SOCK_STREAM):
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\socket.py", line 745, in getaddrinfo
      for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
  socket.gaierror: [Errno 11001] getaddrinfo failed
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "C:\&lt;my workspace&gt;\XMLReq.py", line ##, in &lt;module&gt;
      getResponse(&lt;params&gt;)
    File "C:\&lt;my workspace&gt;\XMLReq.py", line ##, in getResponse
      xml_response = request.urlopen(new_req)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen
      return opener.open(url, data, timeout)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 526, in open
      response = self._open(req, data)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 544, in _open
      '_open', req)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain
      result = func(*args)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 1361, in https_open
      context=self._context, check_hostname=self._check_hostname)
    File "C:\&lt;python storage location&gt;\Python\Python36-32\lib\urllib\request.py", line 1320, in do_open
      raise URLError(err)
  urllib.error.URLError: &lt;urlopen error [Errno 11001] getaddrinfo failed&gt;
  </code></pre>