Exception Handling in Node
The 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.
This are the different methods which 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:// Write Javascript code here// Define divider as a syncrhonous functionvardivideSync =function(x, y) {// if error condition?if( y === 0 ) {// "throw" the error safely by returning itreturnnewError("Can't divide by zero")}else{// no error occurred, continue onreturnx/y}}// Divide 9/3varresult = divideSync(9, 3)// did an error occur?if( resultinstanceofError ) {// handle the error safelyconsole.log("9/3=err", result)}else{// no error occurred, continue onconsole.log("9/3="+result)}// Divide 9/0result = divideSync(9, 0)// did an error occur?if( resultinstanceofError ) {// handle the error safelyconsole.log("9/0=err", result)}else{// no error occurred, continue onconsole.log("9/0="+result)}chevron_rightfilter_noneOutput:

- Exception handling in callback-based( asynchronous) code: In callback-based code, the one of the argument of the callback is err. If an error happens err is the error, if an error doesn’t happen then err is null. The err argument can be followed any number of other arguments.
Example:
// Write Javascript code herevardivide =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 errornext(newError("Can't divide by zero"))}else{// no error occurred, continue onnext(null, x/y)}}divide(9, 3,function(err, result){// did an error occur?if( err ) {// handle the error safelyconsole.log("9/3=err", err)}else{// no error occurred, continue onconsole.log("9/3="+result)}})divide(9, 0,function(err, result){// did an error occur?if( err ) {// handle the error safelyconsole.log("9/0=err", err)}else{// no error occurred, continue onconsole.log("9/0="+result)}})chevron_rightfilter_noneOutput:

- 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:// Write Javascript code here// Definite our Divider Event Emittervarevents = require("events")varDivider =function(){events.EventEmitter.call(this)}require('util').inherits(Divider, events.EventEmitter)// Add the divide functionDivider.prototype.divide =function(x, y){// if error condition?if( y === 0 ) {// "throw" the error safely by emitting itvarerr =newError("Can't divide by zero")this.emit("error", err)}else{// no error occurred, continue onthis.emit("divided", x, y, x/y)}// Chainreturnthis;}// Create our divider and listen for errorsvardivider =newDivider()divider.on('error', function(err){// handle the error safelyconsole.log(err)})divider.on('divided',function(x, y, result){console.log(x+"/"+y+"="+result)})// Dividedivider.divide(9, 3).divide(9, 0)chevron_rightfilter_noneOutput:
Recommended Posts:
- Exception handling in JSP
- Exception Handling in PHP
- Scala | Exception Handling
- File handling in Node.js
- Difference between System Level Exception and Application Level Exception in C#
- Catch and Throw Exception In Ruby
- Node.js | NPM (Node Package Manager)
- Error handling in PHP
- PHP | Basics of File Handling
- Error Handling in Perl
- Perl | File Handling Introduction
- Python | 404 Error handling in Flask
- NodeJs - Handling invalid routes
- Javascript | Error and Exceptional Handling With Examples
- PHP vs. Node.js
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



