2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Preferred method of resolving unhandled exceptions in Node.js?

Preferred method of resolving unhandled exceptions in Node.js?
userimage
Unhandled exceptions in Node.js can be caught at the Process level by attaching a handler for uncaughtException event.

process.on('uncaughtException', function(err) {
  alert('Caught exception: ' + err);
});


However, uncaughtException is a very crude mechanism for exception handling and may be removed from Node.js in the future. An exception that has bubbled all the way up to the Process level means that your application, and Node.js may be in an undefined state, and the only sensible approach would be to restart everything. Another way could be to add a layer between your application and the Node.js process which is called the domain. Domains provide a way to handle multiple different I/O operations as a single group. So, by having your application, or part of it, running in a separate domain, you can safely handle exceptions at the domain level, before they reach the Process level.
Answer is