How to append new information and rethrowing errors in nested functions in JavaScript ?
Last Updated :
28 Jul, 2022
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 throw-statement, 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..!!
Similar Reads
How to throw an error in an async generator function in JavaScript ?
In this article, we will try to understand how to throw an error in a synchronous (abbreviated as "async") generator function in JavaScript with the help of theoretical as well as coding examples. Let us first have a look into the following section which will show us the syntax for declaring an asyn
2 min read
How to Catch an Error from External Library and Store it in a String in JavaScript ?
When using an external library in our code, we may encounter errors. It is essential to catch these errors and handle them gracefully. In some cases, we may want to store the error message in a string for further processing or display it to the user. In this article, we will learn how to catch an er
3 min read
How to transform String into a function in JavaScript ?
In this article, we will see how to transform a String into a function in JavaScript. There are two ways to transform a String into a function in JavaScript. The first and easy one is eval() but it is not a secure method as it can run inside your application without permission hence more prone to th
3 min read
How a Function Returns an Object in JavaScript ?
JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
3 min read
How to Store an Object sent by a Function in JavaScript ?
When a function in JavaScript returns an object, there are several ways to store this returned object for later use. These objects can be stored in variables, array elements, or properties of other objects. Essentially, any data structure capable of holding values can store the returned object. Tabl
2 min read
How to rethrow an exception in JavaScript, but preserve the stack?
Stack trace conveys some portion of the data whenever an exception is thrown. The stack trace is a collection of all the methods used in the program. It starts with the method that throws an exception and ends with the method that catches the exception. In case if an exception is re-thrown, the stac
3 min read
How to call a function that return another function in JavaScript ?
The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer fun
2 min read
How to convert an asynchronous function to return a promise in JavaScript ?
In this article, we will learn how to convert an asynchronous function to return a promise in JavaScript. Approach:Â You need to first declare a simple function (either a normal function or an arrow function (which is preferred)). You need to create an asynchronous function and then further you need
3 min read
How to Merge Two Arrays and Remove Duplicate Items in JavaScript?
Given two arrays, the task is to merge both arrays and remove duplicate items from merged array in JavaScript. The basic method to merge two arrays without duplicate items is using spread operator and the set constructor.1. Using Spread Operator and Set() ConstructorThe Spread Operator is used to me
3 min read
How to create a function that invokes function with partials appended to the arguments in JavaScript ?
In this article, we will learn to create a function that invokes a function with partials appended to the arguments it receives in JavaScript. Approach: We want to implement a function that invokes another function and provides the arguments it received. We can get the result by using (...) the spre
3 min read