Open In App

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 solve our problem:



Syntax: 

class Class_name {
    ...
}

Another syntax shown below indicates or enlightens us on the declaration of any function or method inside the class scope:



class Class_name {
    function_name = () => {
        ...
    }
}

Another shown syntax enlightens us on how to create or instantiate a class’s instance using a variable for calling the declared method inside the class itself:

let object_name = new Class_name ();
object_name.function_name();

After analyzing all the above-shown syntaxes let us have a look over the below-shown example quickly in order to understand all the above-enlightened syntaxes at once.

Example-1: 




<script>
    class DataPrint {
        data_display = () => {
            console.log(
"This article is available on GeeksforGeeks platform...");
        };
    }
 
    let data_object = new DataPrint();
    data_object.data_display();
</script>

Output:

This article is available on GeeksforGeeks platform...

Let us now have a look over the below-shown examples through which we will try to understand how we may intercept or catch all the errors in a class instance itself.

Example 2: 




<script>
    class ThrowingErrorsViaMethods {
        errorThrowingMethod = (error_message) => {
            throw new Error(error_message);
        };
 
        anotherErrorThrowingMethod = (error_message) => {
            throw new Error(error_message);
        };
    }
 
    try {
        let object = new ThrowingErrorsViaMethods();
        object.errorThrowingMethod("Something went wrong...!!");
    } catch (error) {
        console.log(error.message);
    }
 
    try {
        let another_object = new ThrowingErrorsViaMethods();
        another_object.anotherErrorThrowingMethod(
            "No data found!!...");
    } catch (error) {
        console.log(error.message);
    }
</script>

Output:

Something went wrong...!!
No data found!!...

If one wishes to not write multiple try-catch blocks, then there is another technique that will help us in order to prevent the usage of multiple try-catch blocks, which is discussed in another example shown below.

Example 3: 




<script>
    class ThrowingErrorsViaMethods {
        errorThrowingMethod = (error_message) => {
            throw new Error(error_message);
        };
 
        anotherErrorThrowingMethod = (error_message) => {
            throw new Error(error_message);
        };
 
        interceptErrors = (method, error_message) => {
            try {
                method(error_message);
            } catch (error) {
                return error.message;
            }
        };
    }
 
    let object = new ThrowingErrorsViaMethods();
    console.log(
        object.interceptErrors(
            object.errorThrowingMethod,
            "Something went wrong...!!"
        )
    );
 
    let another_object = new ThrowingErrorsViaMethods();
    console.log(
        another_object.interceptErrors(
            another_object.anotherErrorThrowingMethod,
            "No data found!!..."
        )
    );
</script>

Output:

Something went wrong...!!
No data found!!...

Article Tags :