Eliminate incomplete execution of a set of functions due to unforeseen errors

In this article, we will try to understand how we may eliminate incomplete execution of a set of functions due to unforeseen errors with the help of a coding example in JavaScript.
Let us first understand how we may throw as well as catch an error inside a function itself with the help of the following enlightened section that shows the exact syntax which is to use for the same:
Syntax: The following syntax, we may use in order to throw an error in a function, later catch that error in the same function itself (Note here we have used arrow function, the user could also opt for normal function declaration syntax as well):
let function_name = () => {
try {
// error_message is the string value
throw new Error(error_message);
}
catch(error) {
// Do something with this error
}
}
Let us have a look over the following shown example that will help us to understand the above enlightened syntax in a much better and more efficient manner.
Example 1: In this example, we will simply create a function (preferably an arrow function) and inside that, we will create a try/catch block, and then in the try block itself we will throw an error and then catch that error in catch block itself.
Javascript
<script> Â Â Â Â let errorMethod = () => { Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â throw new Error("Please try again later..!"); Â Â Â Â Â Â Â Â } catch (error) { Â Â Â Â Â Â Â Â Â Â Â Â console.log(error.message); Â Â Â Â Â Â Â Â } Â Â Â Â }; Â Â Â Â Â Â errorMethod(); </script> |
Output:
Please try again later..!
Now let us understand our main task which is how to eliminate incomplete execution of a set of functions due to unforeseen errors with a coding example which is enlightened below:
Example 2: In this example, we will create multiple functions which will throw their own respective errors which we will catch in other methods forming a chain of method execution.
Javascript
<script> Â Â Â Â let firstErrorFunction = () => { Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â throw new Error( Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Error thrown by First Function...!!"); Â Â Â Â Â Â Â Â } catch (error) { Â Â Â Â Â Â Â Â Â Â Â Â console.log("Catched error in First Function: "Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + error.message); Â Â Â Â Â Â Â Â Â Â Â Â throw new Error( Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Another error thrown by first function...!!"); Â Â Â Â Â Â Â Â } Â Â Â Â }; Â Â Â Â Â Â let secondErrorFunction = () => { Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â firstErrorFunction(); Â Â Â Â Â Â Â Â } catch (error) { Â Â Â Â Â Â Â Â Â Â Â Â console.log("Catched error in Second Function: "Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + error.message); Â Â Â Â Â Â Â Â Â Â Â Â throw new Error( Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â "Error thrown by second function...!!"); Â Â Â Â Â Â Â Â } Â Â Â Â }; Â Â Â Â Â Â let thirdErrorFunction = () => { Â Â Â Â Â Â Â Â try { Â Â Â Â Â Â Â Â Â Â Â Â secondErrorFunction(); Â Â Â Â Â Â Â Â } catch (error) { Â Â Â Â Â Â Â Â Â Â Â Â console.log("Catched error in Third Function: "Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â + error.message); Â Â Â Â Â Â Â Â } Â Â Â Â Â Â Â Â console.log("No more errors are left for catching..."); Â Â Â Â }; Â Â Â Â Â Â thirdErrorFunction(); </script> |
Output:
Catched error in First Function: Error thrown by First Function…!!
Catched error in Second Function: Another error thrown by first function…!!
Catched error in Third Function: Error thrown by second function…!!
No more errors are left for catching…



