Try-Catch to catch uncaught errors

The first thing that comes to the mind about handling uncaught errors is to use the try-catch on operations that are likely to throw a data error.

process.nextTick(function() {
    try {
      get_data();
    }
    catch(err) {
      console.log(err);
      res.send(500, 'Oops!');
    }
  });

Using try-catch can technically solve the uncaught error problem, but becomes unrealistic in complex situations. Following is an example to demonstrate this fact:

 process.nextTick(function() {
    try {
       process.nextTick(function() {
         set_data();
        });
          get_data();
       }
   catch(err) {
    console.log(err);
    res.send(500, 'Oops!');
    }
  });

Reference

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Express, Node.Js Tagged with: ,