Error : Object
An object thrown when an error occurs.
There are several specific constructor functions for different types of
error conditions:
**EvalError**,
**RangeError**,
**ReferenceError**,
**SyntaxError**,
**TypeError**,
**URIError**,
**NativeError**.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.11
----
Error(message : String) : Error
Same as %%#new_Error_String|**new Error(message)**%%.
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.1.1
----
Error(message : String, options : Object) : Error
Same as %%#new_Error_String_Object|**new Error(message, options)**%%.
Spec:
https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-error-constructor
----
new Error(message : String) : Error
Creates a new **Error** with the specified **message** that describes the error.
var divide = function(x, y) {
if (y === 0) {
throw new Error('divide by zero');
}
return x / y;
};
try {
console.log(divide(10, 0));
}
catch (error) {
console.log(error.name);
console.log(error.message);
}
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.2.1
----
new Error(message : String, options : { cause : Error }) : Error
Creates a new **Error** with the specified **message** that describes the error and
original **cause** of the error.
var validateNumber = function(x) {
if (typeof x !== 'number') {
throw Error(x + ' is not a number');
}
return x;
};
var multiply = function(x, y) {
try {
return validateNumber(x) * validateNumber(y);
}
catch(e) {
throw Error('Unable to multiply', { cause: e });
}
};
try {
console.log(multiply(3, 'abc'));
}
catch (error) {
console.log(error);
console.log('Cause:', error.cause);
}
Spec:
https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-error-constructor
Version:
ECMAScript 2022
----
prototype.message : String
A human readable message that describes the error.
try {
foo;
}
catch (error) {
console.log(error.message);
}
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.4.3
----
prototype.name : String
The type of error.
try {
foo;
}
catch (error) {
console.log(error.name);
}
Spec:
http://www.ecma-international.org/ecma-262/5.1/#sec-15.11.4.2