An exception handling is a try/catch statement. If any exception is raised in the try block in the lines of code, that will be handled by the catch block of a particular try block only.
try { //lines of code } catch (e) {}
Here, “e” is the exception value. Multiple handlers can be added to catch different kinds of errors.
Catching uncaught exceptions:
Uncaught exceptions are those that are not handled by the catch block and remain as they are during the program’s execution, then such scenarios will lead to system crash.
process.on('uncaughtException', err => { console. error('There was an uncaught error,’ err) process.exit(1) //mandatory (as per the Node.js docs) })
Importing the process core model is not required, as it gets automatically injected.
Exceptions with promises:
Different operations can be chained using promises, and errors are handled at the end.
But the question is, how to know exactly where the error is? You really don’t know right. But, errors can be handled in each function that is being called. In the example given above, errors can be handled in each function call(doSomethingX), and inside the error handler will throw a new error and that will be handled by the catch handler.
Now, the error handling part can also be done locally, without doing them in function call. This can be done easily by breaking the chain. We can create a function in each then() and process the exception:
Error handling is the way to express catches and process errors. It is done both synchronous and asynchronous ways. Express has its own error handler, so we don’t need to write on our own.
2. Await throws an error. Justify the statement.
If any promise fails to resolve normally, then await cannot return the result. In such cases, await throws an error.
3. Is node.js a programming language?
No, node.js is not a programming language. But, it makes use of javascript which is a programming language. It also helps web developers to play and build web applications.
4. Name some uses of node.js?
The uses of node.js are:
Server-side programming
Deployed for non-blocking
Creating and deleting server files.
Key Takeaways:
In this article we have learned error handling in node.js. Error can occur anywhere, but it should be handled properly. Like we have seen above, in a code, there can be many try catch blocks but the error on one particular try will be handled by its catch block only. Failing to do so, will result in a system crash.
Keeping the theoretical knowledge at our fingertips helps us get about half the work done. To gain complete understanding, practice is a must. To gain complete understanding, visit our page.
By: Pradipta Choudhury
Live masterclass
Amazon PowerBI & AI Essentials: Data Visualization Tips
by Abhishek Soni
15 May, 2025
01:30 PM
Amazon SDE Resume Tips: Get Noticed, Get Hired
by Shantanu Shubham
12 May, 2025
01:30 PM
Microsoft Data Analytics Interview: How to Clear It?
by Prerita Agarwal
13 May, 2025
01:30 PM
Ace Data Interviews: Master Analytics with AI Tools
by DP Aurosish
17 May, 2025
07:30 AM
Crack Google SDE Interviews: Strategies to Master DSA
by Saurav Prateek
14 May, 2025
01:30 PM
Amazon PowerBI & AI Essentials: Data Visualization Tips