Node.js Best Practice Exception Handling
Asked 07 September, 2021
Viewed 2.9K times
  • 60
Votes

I just started trying out node.js a few days ago. I've realized that the Node is terminated whenever I have an unhandled exception in my program. This is different than the normal server container that I have been exposed to where only the Worker Thread dies when unhandled exceptions occur and the container would still be able to receive the request. This raises a few questions:

  • Is process.on('uncaughtException') the only effective way to guard against it?
  • Will process.on('uncaughtException') catch the unhandled exception during execution of asynchronous processes as well?
  • Is there a module that is already built (such as sending email or writing to a file) that I could leverage in the case of uncaught exceptions?

I would appreciate any pointer/article that would show me the common best practices for handling uncaught exceptions in node.js

11 Answer