'Where are errors stored?
I was wondering where errors are stored in Javascript. An error is thrown and passed to catch() in the MDN example below. My question is how does catch() get the error object? Where is the thrown error stored to be a parameter for catch() later?
try {
nonExistentFunction();
} catch (error) {
console.error(error);
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
}
Solution 1:[1]
The error is thrown by the javascript runtime because you are calling an undefined function. Something like this happens under the hood
const func = () => {
try {
throw new ReferenceError(
"nonExistentFunction is not defined"
);
} catch (err) {
console.log(err);
}
};
func();
catch just capture this error from try scope
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | lex |
