How to intercept all errors in a class instance ?
Last Updated :
10 Aug, 2022
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!!...
Similar Reads
How to create custom errors in JavaScript ? In this article, we will learn to create custom errors with some examples. Errors represent the state of being wrong in condition. Javascript handles a predefined set of errors by itself but if you want to create your own error handling mechanism you can do that with custom errors functionality avai
2 min read
How to Solve Class Cast Exceptions in Java? An unexcepted, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating at the U.S.A. At runtime, if the remote fi
3 min read
How to Ignore an Exception and Proceed in Python There are a lot of times when in order to prototype fast, we need to ignore a few exceptions to get to the final result and then fix them later. In this article, we will see how we can ignore an exception in Python and proceed. Demonstrate Exception in Python Before seeing the method to solve it, l
3 min read
Is there a TRY CATCH command in Bash? Bash is a powerful scripting language used in the Unix and Linux environments, primarily for automating tasks and scripting. While Bash doesn't have a built-in 'TRY CATCH' command like some other programming languages, you can achieve similar functionality using a combination of 'trap', the '-x' fla
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