In JavaScript Error Handling is used to find the errors in the code so, that the coder can find the particular issue to resolve it. Here are the different approaches to handle errors in JavaScript.
1. Using try catch and finally statement
The try, catch, and finally blocks are used for error handling. The try block tests code, catch handles errors, and finally runs at the end no matter what the error was.
- In the above code the constant variable a is assigned a variable b which is not defined anywhere.
- In this case Reference error will be thrown as there is no memory allocated for the variable b which makes it difficult for JavaScript to refer the memory location of b
2. Explicitly causing errors using throw statement
The throw statement in JavaScript is used to explicitly throw error's in JavaScript and then these explicitly caused errors are caught by the catch block.
- In this code in the try block a error message 'Error is caused due to throw statement' is caused explicitly with the use of throw keyword
- In the catch part of this code the error is caught and is then printed on to the console
3. Errors using Error Object
The Error Object in JavaScript provides a complete object which contain's the message thrown by the code and the user can find out various aspect's from it like the error message, error name and error stack means at which part in the call stack the error is detected.
- In the try block the function A is called which in turn calls function B which in turn calls the function C.
- The error is then caught by the catch block and then the error.message prints the error message from the error object.
- The error.stack prints the stack trace which means it will print the function in which the error is caused and will also print all the function's that had led to the call of that function.
4. Handling Asynchronous Errors with Promises
Handling asynchronous errors with Promises involves using .catch() to catch errors that occur during asynchronous operations, ensuring proper error handling in non-blocking code.
- The fetch method initiates an HTTP request.
- If the request fails (e.g., due to an invalid URL), the error is passed to the catch block.
- The catch block logs the error, ensuring graceful failure handling.
5. Handling Asynchronous Errors with async/await
The try...catch block is commonly used with async/await in asynchronous code to handle errors that may occur during the execution of asynchronous functions, allowing you to catch and manage exceptions in a clean and readable way.