How to make a function to perform try/check without causing an error ?
Last Updated :
08 Aug, 2022
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 containing several syntaxes which we will use in order to solve our above-illustrated problem easily.
Syntax:
try {
// do something...
} catch(error) {
// do something with error...
}
Another shown syntax below helps us to create a function or method (this is the arrow function technique which is a highly recommended one, we may also opt for normal function declaration):
let function_name = () => {
// do something inside the function scope...
}
Let us first have quick look over the below-shown example which will help us to understand all the syntaxes efficiently.
Example 1: In this example, we will create two methods (using the above-shown syntaxes) that will individually throw different errors, and then using multiple try-catch blocks we will fetch and handle the errors respectively.
JavaScript
<script>
let firstRejectedMethod = (rejectionMessage) => {
throw new Error(rejectionMessage);
};
let secondRejectedMethod = (rejectionMessage) => {
throw new Error(rejectionMessage);
};
try {
firstRejectedMethod("Error 404!!...");
} catch (error) {
console.log("First Rejected Message: "
+ error.message);
}
try {
secondRejectedMethod("Something went wrong!!...");
} catch (error) {
console.log("Second Rejected Message: "
+ error.message);
}
</script>
Output:
First Rejected Message: Error 404!!...
Second Rejected Message: Something went wrong!!...
Now it's time to look over our problem statement, that is, how to make a function to perform try/check (or try-catch operation) without causing an error with the help of an example shown below.
Example 2: In this example, we will again two methods, as created in the previous example itself, which will throw different errors respectively, and then we will create one more function with which we will handle our errors with one try-catch block only instead of multiple ones.
JavaScript
<script>
let firstRejectedMethod = (rejectionMessage) => {
throw new Error(rejectionMessage);
};
let secondRejectedMethod = (rejectionMessage) => {
throw new Error(rejectionMessage);
};
let InterceptRejectedMessagesMethod = (callback, content) => {
try {
callback(content);
} catch (error) {
return error.message;
}
};
let first_rejected_message = InterceptRejectedMessagesMethod(
firstRejectedMethod,
"Error 404!!..."
);
let second_rejected_message = InterceptRejectedMessagesMethod(
secondRejectedMethod,
"Something went wrong!!..."
);
console.log("First Rejected Message: "
+ first_rejected_message);
console.log("Second Rejected Message: "
+ second_rejected_message);
</script>
Output:
First Rejected Message: Error 404!!...
Second Rejected Message: Something went wrong!!...
Similar Reads
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 throw an error in an async generator function in JavaScript ? In this article, we will try to understand how to throw an error in a synchronous (abbreviated as "async") generator function in JavaScript with the help of theoretical as well as coding examples. Let us first have a look into the following section which will show us the syntax for declaring an asyn
2 min read
How to Validate Input to a Function Error in R In this article, we will examine various methods for how to validate input to the function by using R Programming Language. How to validate input to the function?Validating input to a function in R is crucial for ensuring that your code behaves as expected and can handle various types of input grace
5 min read
How to re-write the code sample without try/catch block ? Errors almost certainly occur when executing JavaScript code. These problems can arise due to a programming error, incorrect input, or an issue with the program's logic. However, all mistakes may be solved, and to do so, we have different statements but the most commonly used is "try and catch". How
4 min read
How to catch a NumPy warning like an exception in Python An N-dimensional array that is used in linear algebra is called NumPy. Whenever the user tries to perform some action that is impossible or tries to capture an item that doesn't exist, NumPy usually throws an error, like it's an exception. Similarly, in a Numpy array, when the user tries to perform
3 min read