Skip to content
Related Articles

Related Articles

JavaScript/TypeScript: Standard way to keep the cause of an Error

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 02 Aug, 2022
View Discussion
Improve Article
Save Article

In this article, we will try to understand that what’s the standard way to keep the cause of an Error in JavaScript (or in TypeScript) with the help of certain coding examples.

Since there is no such provision provided in JavaScript/TypeScript where directly we may be able to find out the cause of an error (like no such property or in-built method exists in either of the languages), therefore we have to manually do some changes while throwing some errors one after the other.

We will understand our problem statement with the help of an example shown below.

Example 1:

  • In this example we will create a function, inside that function we will create a nested set of try/catch blocks. 
  • Then we will throw an error in nested one and then catch that thrown error via catch statement. 
  • Then later we will throw another error which will be cached in the outer catch block written after the outer try block.

Javascript




<script>
    let errorFunction = () => {
        try {
            try {
                throw new Error(
                    "Second Error: Something went wrong..!!");
            } catch (error2) {
                console.log(error2);
                throw new Error("First Error: Error 404!!...");
            }
        } catch (error1) {
            console.log("-------------------------");
            console.log(error1);
        }
    };
  
    errorFunction();
</script>

Output:

 

Now, as we may visualize in the browser’s console’s output the first error doesn’t keep any reference to the second error so that is actually our problem statement which we will solve in another example shown below.

Example 2:

  • In this example, we will take the same code implemented in the previous example itself but with slight changes which will be implemented. 
  • Here we will use another type of error which is though defined but still for the sake of code implementation we will define it, namely, TrackError. 
  • Inside which we will pass the reference of the second error which later will be captured by the catch statement implemented in order to catch the first error itself. 
  • That’s how we may keep the reference of an error and in the output also we will witness an error called ReferenceError.

Javascript




<script>
    let errorFunction = () => {
        try {
            try {
                throw new Error(
                    "Second Error: Something went wrong..!!");
            } catch (error2) {
                console.log(error2);
                throw new TrackError(
                    "First Error: Error 404!!...", error2);
            }
        } catch (error1) {
            console.log("-------------------------");
            console.log(error1);
        }
    };
    errorFunction();
</script>

Output:

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!