Skip to content
Related Articles

Related Articles

The ‘new’ operator in Javascript for Error Handling

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Aug, 2022
View Discussion
Improve Article
Save Article

In this article, we will see new operator usage during error throwing as well as handling the error() method in JavaScript using theoretical as well as coding examples.

The new operator: The new operator is actually a special operator that is used along with the error() method which is the method for the error class used to instantiate any specific error in order to declare any newly user-defined error. Generally, we implement these things under the try-catch block structure. We throw an error in the try block and catch that thrown error in the catch block.

Let us understand the above-said facts thoroughly with the help of certain syntaxes. The following syntax illustrates to us how to use the new operator with the error() method in order to instantiate user-defined error.

Syntax:

new Error (error_message); 

Note: The error_message could be anything in the form of a string.

Another below-illustrated syntax illustrates how we may use the above syntax in order to throw an error.

throw new Error (error_message);  

Another syntax shown below illustrates how to implement the above two syntaxes under the try-catch block itself.

try {
    throw new Error (error_message);
} catch(error) {
    // handle this error...    
}

Example 1: 

  • This example is the simplest and easiest example that will show the usage of all the above-shown syntaxes in a detailed and effective manner. 
  • In this example, we will simply create a try-catch block inside which we will perform our error throwing and error handling process. 
  • Inside try-block we will throw an error message using the same syntax which would be in the form of a string itself of any length. 
  • In the catch block, we will catch the thrown error using the above-illustrated syntax itself, and we will use the console.log() method in order to print the error in the console.

Javascript




<script>
    try {
        throw new Error("Something went wrong!!..");
    }
    catch (error) {
        console.log(error.message);
    }
</script>

Output:

Something went wrong!!..

Example 2: 

  • In this example, we will see all the above syntaxes usage inside a function and we will create another separate function that is responsible for throwing errors. 
  • There will be only one function that will handle the error thrown by several other functions and that one function will be declared using async-keyword which is responsible for handling all the asynchronous operations on itself. 
  • During the time of calling other error-thrown methods, we will use await-keyword so that all the data could be fetched properly at once only.

Javascript




<script>
    let firstErrorThrowingFunction = () => {
        throw new Error("Something went wrong!!!....");
    };
  
    let secondErrorThrowingFunction = () => {
        throw new Error("Please try again later...!!");
    };
  
    let errorHandlerMethod = async () => {
        try {
            await firstErrorThrowingFunction();
        } catch (error) {
            console.log(error.message);
        }
  
        try {
            await secondErrorThrowingFunction();
        } catch (error) {
            console.log(error.message);
        }
    };
    errorHandlerMethod();    
</script>

Output:

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!