Open In App

How to intercept all errors in a class instance ?

Last Updated : 10 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  • In this example, we will simply create a class with a particular name and inside the class, we will declare a method or a function that will contain our data which we will print as the console’s output. 
  • Later we will instantiate a class‘s instance (that is class‘s object which is responsible for calling our method) and using it we will call our method which is declared inside the class‘s scope itself.

Javascript




<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: 

  • In this example, we will simply create a class using the same syntax (as shown above) and that class will further contain two separate methods that will throw different errors respectively. 
  • Then we create multiple try-catch blocks which are responsible for fetching as well as handling all the errors thrown by each function individually. 
  • Inside the try-block, we will capture or fetch the error thrown by method and then inside the catch block, we will handle or print the error as an output that was captured in the try-block itself.

Javascript




<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: 

  • In this article, we will try to prevent the usage of multiple try-catch blocks using a special technique that uses a concept called callbacks itself. 
  • Here again, we will create a class and inside that class, we will again two separate methods that will different errors respectively (as we have seen in the previous examples too). 
  • Apart from these two functions, we will create one function or a method inside the class itself that will accept two parameters one is a method (which is actually the callback itself, since that will call those two methods in itself) and the other is error_message of the type which will explicitly be passed in this third method. 
  • When these parameters are passed, we will create a try-catch block inside the third method itself and inside the try-block, we will use a method or callback that will call the other methods containing the passed-in error_message, and further inside the catch-block of the third method, we will handle or print the errors as output respectively. 
  • Then at last we will create two separate objects for the same class itself through which we will call each time the third method which is responsible for both capturing/fetching as well as handling all the errors thrown by the two methods (which are passed in as parameters) and then we will see all the errors in and as the output itself.

Javascript




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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads