Open In App

How to increase multiple try-catch readability in JavaScript ?

In this article, we will try to understand how we may increase the readability of multiple try/catch blocks in JavaScript with the help of certain coding examples.

Let us first understand the fact that how we may declare a simple try/catch block (though multiple in number) and also how we may throw an error with the help of the following enlightened syntaxes.



Syntax: Following shown is the syntax by which we may declare multiple try/catch blocks in JavaScript:

 try {
 
    // Here we will add all those variables
    // or methods which tends to or responsible
    // for causing error or exception laterwards
}

catch(error_variable){

    // Here we will do something with the 
    // error_variable either handle it or
    // print it as per need
}
...

// We may add many more such blocks 
// as per the requirement...

The following shown syntax shows show we may throw an error in JavaScript:



thrown new Error(errorMessage); 
// errorMessage is in the form of string itself

Let us now have a look over the following enlightened section containing coding examples where at first, we will see how we may create multiple try/catch blocks, and then laterwards we will see how we may avoid such blocks in order to increase their readability to any of its users.

Example 1:  




<script>
    let first_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let second_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let third_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let catchingErrors = () => {
        try {
            let result = first_error_function(
                "Error 404 !!....");
            console.log(result);
        } catch (error) {
            console.log(error.message);
        }
  
        try {
            let result = second_error_function(
                "Something went wrong!!....");
            console.log(result);
        } catch (error) {
            console.log(error.message);
        }
  
        try {
            let result = third_error_function(
                "Please try again later!!....");
            console.log(result);
        } catch (error) {
            console.log(error.message);
        }
    };
  
    catchingErrors();
</script>

Output:

Error 404 !!....
Something went wrong!!....
Please try again later!!....

Example 2: 




<script>
    let first_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let second_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let third_error_function = (errorMessage) => {
        throw new Error(errorMessage);
    };
  
    let catchingAllErrors = (callback, content) => {
        try {
            callback(content);
        } catch (errorMessage) {
            return errorMessage;
        }
    };
  
    let main_function = () => {
        let error_1 = catchingAllErrors(
            first_error_function, "Error 404!!...");
          
        let error_2 = catchingAllErrors(
            second_error_function,
            "Something went wrong!!..."
        );
      
        let error_3 = catchingAllErrors(
            third_error_function,
            "Please try again later!!...."
        );
      
        console.log("First Catched Error: " + error_1);
        console.log("Second Catched Error: " + error_2);
        console.log("Third Catched Error: " + error_3);
    };
  
    main_function();
</script>

Output:

First Catched Error: Error: Error 404!!...
Second Catched Error: Error: Something went wrong!!...
Third Catched Error: Error: Please try again later!!....

Article Tags :