VOOZH about

URL: https://www.geeksforgeeks.org/node-js/exception-handling-in-node-js/

⇱ Exception Handling in Node.js - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Exception Handling in Node.js

Last Updated : 11 Apr, 2023

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: 

Output: 

👁 Image

  • 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: 

Output: 
 👁 Image

  • 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: 

Output: 

👁 Image

Comment
Article Tags:

Explore