Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to append new information and rethrowing errors in nested functions in...

How to append new information and rethrowing errors in nested functions in JavaScript ?

In this article, we will see how we may append new information and rethrow errors in nested functions in JavaScript with the help of certain theoretical explanations & understand them through the illustrations.

This article is divided into 2 sections, i.e. in the 1st section, we’ll understand how to append the new information while throwing an error, along with knowing how to re-throw an error after the initial thrown error. In the 2nd section, we’ll how to rethrow the error in a nested function with appended information.

How to append new information while throwing an error?

In order to append new information along with the error message thrown inside the throwstatement, the concatenation operator (“+”) will be used which helps to concatenate the error message along with the appended information.

Syntax: With the help of the following syntax, we may append some new information while throwing an error in JavaScript:

throw new Error (error_message + "appended_information_in_string_format");

Let’s understand the above syntax with the help of an example described below.

Example 1: In this example, we will simply create a try/catch block & inside the try block, we will throw an error along with the appended information, which will be caught inside the catch block itself.

Javascript




<script>
    try {
        throw new Error("Please try again later!!.. "
            + "Site not working..!!");
    } catch (error) {
        console.log(error.message);
    }
</script>


Output:

Please try again later!!.. Site not working..!!

How to rethrow an error after an initially thrown error?

In order to rethrow an error, JavaScript doesn’t provide any special keyword for the same. In order to initiate this process, we must have to throw an error inside the catch block itself, so that after catching the previous error in itself, it could throw an error that will be cached later using another catch block itself.

Syntax:

try {
    throw new error (error_message);
}
catch (error){
    // do something with the error message itself
    // Here, we will again throw an error in
    // catch block itself
    throw new Error (new_error_message) 
}

Example 2: In this example, we will again use a try/catch block as we have used in the previous section’s example. Here, in the catch block, we will again throw an error that we will catch in a different catch block. Also, we will wrap the complete try/catch block inside the try block itself (making the initial try/catch block a nested block) and will put a catch block after this try block

Javascript




<script>
    try {
        try {
            throw new Error("Error occurred...!!");
        } catch (error) {
            console.log(error.message);
            throw new Error("Please try again later..!!");
        }
    } catch (error) {
        console.log(error.message);
    }
</script>


Output:

Error occurred...!!
Please try again later..!!

Now, we’ll understand how to perform appending of new information and rethrowing errors in nested functions themselves. Here, we will create two functions that will throw two different errors respectively, and these errors will be caught later in other functions. After creating these two functions, we need to create the 3rd function and with the help of this function, we will catch all the errors thrown by the other two functions. Also, the 3rd function will throw its own error message followed by the appended information, which will be initiated with the help of the syntaxes as shown in the above sections.

Later, after catching those errors in the catch block, we will throw an error in the catch block, which will be displayed two times in the output. Also, we will wrap that try/catch block that was created inside the 3rd function inside another single try block, and then we will wrap that try block with the catch block which is actually placed just after it. 

The above steps are done in order to ensure that both errors are thrown by 2 different functions that could be easily cached inside the catch block, along with the 3rd function thrown error with appended information. Then, we will match a try/catch block, after all the above tasks, and inside the try block, we will call the 3rd function while passing other functions one after the other as a parameter in the 3rd function itself. Then, in the end, we will catch all the errors one after the other and display those two errors in the console.

Example: This example illustrates rethrowing the error in a nested function with appended information.

Javascript




<script>
    let firstErrorFunction = () => {
        throw new Error("Please try again later!!..");
    };
 
    let secondErrorFunction = () => {
        throw new Error("Something went wrong!!..");
    };
 
    let thirdErrorFunction = (func) => {
        try {
            try {
                func();
            } catch (error) {
                console.log(error.message);
                throw new Error("Error 404!!..."
                    + "File not found..!!");
            }
        } catch (error) {
            console.log(error.message);
        }
    };
 
    try {
        thirdErrorFunction(firstErrorFunction);
        thirdErrorFunction(secondErrorFunction);
    } catch (error) {
        console.log(error.message);
    }
</script>


Output:

Please try again later!!..
Error 404!!...File not found..!!
Something went wrong!!..
Error 404!!...File not found..!!

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments