Error types
suggest changeThe long-standing standard Error subclasses are:
EvalError. The specification still defines it. Current engines rarely throw it foreval().RangeError. A numeric value is outside the allowed range.ReferenceError. A binding cannot be resolved, or alet/constbinding is used in its temporal dead zone.SyntaxError. Source text cannot be parsed.eval(),JSON.parse(), and theFunctionconstructor can throw it at run time.TypeError. A value has the wrong type for an operation.URIError.encodeURI()ordecodeURI()received a malformed argument.
AggregateError is a later standard subclass, used when several errors are reported together. InternalError is not standard. Firefox uses it for engine limits such as too much recursion.
If you are implementing error handling mechanism you can check which kind of error you are catching from code.
try {
throw new TypeError();
}
catch (e){
if(e instanceof Error){
console.log('instance of general Error constructor');
}
if(e instanceof TypeError) {
console.log('type error');
}
}
In such case e will be an instance of TypeError. All error types extend the base constructor Error, therefore it’s also an instance of Error.
instanceof Error does not tell these types apart. It still matters when the catch might receive a non-Error value. instanceof also fails across realms, such as an error from another window.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents