title: Try-Catch not handling errors with an https.get request in Node
body: <p>I have an https.get request in Node for which I need to handle the errors - preferably in a try-catch block. For instance, when the url is incorrect</p>
  
  <p>I have tried wrapping the https.get block in a try catch and I have tried handling with res.on('error'). It seems as if in both instances the error doesn't reach the error handling block. </p>
  
  <pre><code>const https = require('https');
  
  const hitApi = () =&gt; {
  
      const options = {
          "hostname": "api.kanye.rest"
      };
  
      try {
          https.get(options, res =&gt; {
  
              let body = '';
  
  
              res.on('data', data =&gt; {
                  body += data;
              });
  
              res.on('end', () =&gt; {
                  body = JSON.parse(body);
                  console.dir(body);
              });
  
          });
  
      } catch (error) {
          throw error;
      }
  }
  
  hitApi();
  </code></pre>
  
  <p>If I change the url to a nonexistent API (e.g. api.kaye.rest) I would expect to see a handled e.rror response. Instead I see 'Unhandled error event'</p>