JavaScript/TypeScript: Standard way to keep the cause of an Error Last Updated : 02 Aug, 2022 Comments Improve Suggest changes Like Article Like Report 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: Comment More infoAdvertise with us Next Article JavaScript/TypeScript: Standard way to keep the cause of an Error A amansingla Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions JavaScript-Statements Similar Reads JavaScript Errors Throw and Try to Catch JavaScript uses throw to create custom errors and try...catch to handle them, preventing the program from crashing. The finally block ensures that code runs after error handling, regardless of success or failure.throw: Used to create custom errors and stop code execution.try...catch: Allows you to c 3 min read How to Declare a Function that Throws an Error in TypeScript ? In this article, we are going to declare the functions that throw errors in TypeScript. A function can throw an error if the logic is not correct or you can deliberately declare the function that always throws an error. A function can be created with the different return types with the required type 2 min read JavaScript Error.prototype.stack Property To handle any error which occurs while execution, the methods of error handling are used. In these methods, the type of error where it is raised and reasons are given in a definite order or sequence. Stack Structures are used to store these sequences. In JavaScript, all errors and their details are 3 min read How to catch all JavaScript errors and send them to server ? Today there are a large number of Web APIs available. One of which is GlobalEventHandlers, an OnErrorEventHandler to be called when the error is raised. The onerror property of the GlobalEventHandlers is an EventHandler that processes error events. This is great for catching exceptions that never oc 2 min read JavaScript Error name Property In JavaScript, the Error name property is used to set or return the name of an error. Syntax: errorObj.name Property values: This property contains six different values as described below: SyntaxError: It represents a syntax error.RangeError: It represents an error in the range.ReferenceError: It re 2 min read Like