Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptJavaScript/TypeScript: Standard way to keep the cause of an Error

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

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:

 

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!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments