Unhanded Exception Management

suggest change

Because Node.js runs on a single process uncaught exceptions are an issue to be aware of when developing applications.

Silently Handling Exceptions

Most of the people let node.js server(s) silently swallow up the errors.

process.on('uncaughtException', function (err) {
  console.log(err);
});

This is bad, it will work but:


Returning to Initial state

In case of an “ uncaughtException “ it is good to restart the server and return it to its initial state, where we know it will work. Exception is logged, application is terminated but since it will be running in a container that will make sure that the server is running we will achieve restarting of the server ( returning to the initial working state ) .

npm install forever -g

forever start app.js

Reason why is it started and why we use forever is after the server is terminated forever process will start the server again.
process.on('uncaughtException', function (err) {
    console.log(err);
// some logging mechanisam
// ....        

process.exit(1); // terminates process
});

On a side note there was a way also to handle exceptions with Clusters and Domains.

Domains are deprecated more information here.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents