Eliminate incomplete execution of a set of functions due to unforeseen errors
Last Updated :
02 Aug, 2022
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...
Similar Reads
How to Create Custom Errors using New Function in Golang? Custom errors are those that can be defined by the user. For example, if one writes a function that is used to divide two numbers, which is to be used in multiple places, the user could create a custom error that can be set whenever division by zero is encountered, for it could prove to be fatal in
2 min read
How to Declare a Function that Throws an Error in TypeScript ? In this article, we are going to declare the functions that throw errors in TypeScript. A function can throw an error if the logic is not correct or you can deliberately declare the function that always throws an error. A function can be created with the different return types with the required type
2 min read
How to make a function to perform try/check without causing an error ? In this article, we will try to understand how to make a function perform a try/check (or basically try-catch operation) without causing an error, with the help of certain theoretical explanations as well as coding examples in JavaScript. Let us first have a look at the below-shown section containin
3 min read
Visual studio code doesn't give out error if parents function didn't close correctly Visual Studio Code (VS Code) is a popular code editor used by many developers worldwide. It provides several features and functionalities for developers to write efficient and error-free code. However, sometimes it can be frustrating when an error in your code doesn't display in the editor, especial
4 min read
How to intercept all errors in a class instance ? In this article, we will try to understand how we could easily intercept or catch all the errors in a class instance with the help of theoretical explanations as well as coding examples in JavaScript. Let us first understand the below section shows several syntaxes which we will use in order to solv
4 min read