How to resolve unhandled exceptions in Node.js ? Last Updated : 20 May, 2020 Comments Improve Suggest changes Like Article Like Report There are two approaches to resolve unhandled exceptions in Node.js that are discussed below: Approach 1: Using try-catch block: We know that Node.js is a platform built on JavaScript runtime for easily building fast and scalable network applications. Being part of JavaScript, we know that the most prominent way to handle the exception is we can have try and catch block. Example: javascript try { // The synchronous code that // we want to catch thrown // errors on var err = new Error('Hello') throw err } catch (err) { // Handle the error safely console.log(err) } Note: However, be careful not to use try-catch in asynchronous code, as an asynchronously thrown error will not be caught. javascript try { setTimeout(function() { var err = new Error('Hello') throw err }, 1000) } catch (err) { // Example error won't be caught // here... crashing our application // hence the need for domains } Approach 2: Using Process: A good practice says, you should use Process to handle exception. A process is a global object that provides information about the current Node.js process. The process is a listener function that is always listening to the events. Few events are: Disconnect Exit Message Multiple Resolves Unhandled Exception Rejection Handled Uncaught Exception Warning The most effective and efficient approach is to use Process. If any uncaught or unhandled exception occurs in your code flow, that exception will be caught in code shown below: Example: javascript process.on('uncaughtException', function(err) { // Handle the error safely console.log(err) }) The above code will be able to handle any sort of unhandled exception which occurs in Node.js. Comment More infoAdvertise with us Next Article How to resolve unhandled exceptions in Node.js ? S ShivanshBajpai Follow Improve Article Tags : Web Technologies Node.js Node.js-Misc Similar Reads How Should Unhandled Errors Preferably be Resolved in Node.js ? Unhandled errors in Node.js can lead to unexpected application crashes, data corruption, and poor user experiences. Effectively managing these errors is crucial for building robust and reliable applications. In this article, we'll explore best practices for handling unhandled errors in Node.js, ensu 5 min read How to Exit Process in Node.js ? In this article, we will see how to exit in NodeJS application. There are different types of methods to exit in Nodejs application, here we have discussed the following four methods. Table of Content Using ctrl+C keyUsing process.exit() FunctionUsing process.exitCode variableUsing process.on() Funct 3 min read Exception Handling in Node.js Exception handling refers to the mechanism by which the exceptions occurring in a code while an application is running is handled. Node.js supports several mechanisms for propagating and handling errors. There are different methods that can be used for exception handling in Node.js: Exception handl 3 min read How to prevent TypeError in Node.js ? In this article we are going to learn about that how can we prevent the code from crashing if in case there is any kind of TypeError and how to handle them in node.js. There are various Reasons for TypeError: Calling a value a functionCalling an object a functionIterating a number using a conditiona 3 min read How to restart Node.js application when uncaught exception happen ? In this article, we are going to learn, about restarting a Node.js application when an uncaught exception happens. For this, we are going to use the pm2 module.Approach: Let's see the approach step by step:Step 1: Install the pm2 module and use it to start the server.Step 2: When an uncaught excepti 2 min read Like