title: How do I use a try/catch block for a MongoDB connection
body: <p>I want to check a connection to my MongoDB, should it be down I want to send myself an email. I can't seem to get my head around the try/catch element of what I want to do - I am fairly new to JavaScript.</p>
  
  <p>Here is my code so far: </p>
  
  <pre><code>const MongoClient = require('mongodb').MongoClient;
  const url = "mongodb://myserver:port/";
  
  function check() {
      MongoClient.connect(url, {useNewUrlParser: true}, async (err, db) =&gt; {
              if (err) throw err;
              console.log(err)
  
          try {
              if(err == "MongoNetworkError") throw "No connection"
          }
  
          catch(err) {
              console.log("no connection")
          }
  
  })
  }
  </code></pre>
  
  <p>When a connection is established it prints <code>null</code>, when I trigger the error by turning off the server it doesn't print <code>"no connection"</code>. </p>
  
  <p>Thanks for any help</p>