Exception Handling in Node.js Last Updated : 11 Apr, 2023 Comments Improve Suggest changes 3 Likes Like Report 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 handling in synchronous code: If an error occurs in a synchronous code, return the error. Example: javascript // Write Javascript code here // Define divider as a synchronous function let divideSync = function (x, y) { // if error condition? if (y === 0) { // "throw" the error safely by returning it return new Error("Can't divide by zero") } else { // no error occurred, continue on return x / y } } // Divide 9/3 let result = divideSync(9, 3) // did an error occur? if (result instanceof Error) { // handle the error safely console.log("9/3=err", result) } else { // no error occurred, continue on console.log("9/3=" + result) } // Divide 9/0 result = divideSync(9, 0) // did an error occur? if (result instanceof Error) { // handle the error safely console.log("9/0=err", result) } else { // no error occurred, continue on console.log("9/0=" + result) } Output: Exception handling in callback-based( asynchronous) code: In callback-based code, one of the arguments of the callback is erred. If an error happens err is the error, if an error doesn't happen then err is null. The err argument can be followed by any number of other arguments. Example: javascript // Write Javascript code here let divide = function (x, y, next) { // if error condition? if (y === 0) { // "throw" the error safely by calling the completion callback // with the first argument being the error next(new Error("Can't divide by zero")) } else { // no error occurred, continue on next(null, x / y) } } divide(9, 3, function (err, result) { // did an error occur? if (err) { // handle the error safely console.log("9/3=err", err) } else { // no error occurred, continue on console.log("9/3=" + result) } }) divide(9, 0, function (err, result) { // did an error occur? if (err) { // handle the error safely console.log("9/0=err", err) } else { // no error occurred, continue on console.log("9/0=" + result) } }) Output: Exception handling in eventful code: In an eventful code, the error may happen anywhere. So instead of throwing the error, fire the error event instead. Example: javascript // Write Javascript code here // Definite our Divider Event Emitter const events = require("events") let Divider = function () { events.EventEmitter.call(this) } require('util').inherits(Divider, events.EventEmitter) // Add the divide function Divider.prototype.divide = function (x, y) { // if error condition? if (y === 0) { // "throw" the error safely by emitting it var err = new Error("Can't divide by zero") this.emit("error", err) } else { // no error occurred, continue on this.emit("divided", x, y, x / y) } // Chain return this; } // Create our divider and listen for errors let divider = new Divider() divider.on('error', function (err) { // handle the error safely console.log(err) }) divider.on('divided', function (x, y, result) { console.log(x + "/" + y + "=" + result) }) // Divide divider.divide(9, 3).divide(9, 0) Output: Create Quiz Comment C chaitanyashah707 Follow 3 Improve C chaitanyashah707 Follow 3 Improve Article Tags : Web Technologies Node.js Explore Introduction & Installation NodeJS Introduction3 min readNode.js Roadmap: A Complete Guide6 min readHow to Install Node.js on Linux6 min readHow to Install Node.js on Windows5 min readHow to Install NodeJS on MacOS6 min readNode.js vs Browser - Top Differences That Every Developer Should Know6 min readNodeJS REPL (READ, EVAL, PRINT, LOOP)4 min readExplain V8 engine in Node.js7 min readNode.js Web Application Architecture3 min readNodeJS Event Loop5 min readNode.js Modules , Buffer & StreamsNodeJS Modules5 min readWhat are Buffers in Node.js ?4 min readNode.js Streams4 min readNode.js Asynchronous ProgrammingAsync Await in Node.js3 min readPromises in NodeJS7 min readHow to Handle Errors in Node.js ?4 min readException Handling in Node.js3 min readNode.js NPMNodeJS NPM6 min readSteps to Create and Publish NPM packages7 min readIntroduction to NPM scripts2 min readNode.js package.json4 min readWhat is package-lock.json ?3 min readNode.js Deployments & CommunicationNode Debugging2 min readHow to Perform Testing in Node.js ?2 min readUnit Testing of Node.js Application5 min readNODE_ENV Variables and How to Use Them ?2 min readDifference Between Development and Production in Node.js3 min readBest Security Practices in Node.js4 min readDeploying Node.js Applications5 min readHow to Build a Microservices Architecture with NodeJS3 min readNode.js with WebAssembly3 min readResources & ToolsNode.js Web Server6 min readNode Exercises, Practice Questions and Solutions4 min readNode.js Projects9 min readNodeJS Interview Questions and Answers15+ min read Like