Open In App

How to make a function to perform try/check without causing an error ?

Improve
Improve
Like Article
Like
Save
Share
Report

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!!...


Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads