JS Error Object
JS Error Object
Parameters
message
Optional. Human-readable description of the error.
fileName
Optional. The value for the fileName property on the
created Error object. Defaults to the name of the file containing the
code that called the Error() constructor.
lineNumber
Optional. The value for the lineNumber property on the
created Error object. Defaults to the line number containing
the Error() constructor invocation.
Description
Runtime errors result in new Error objects being created and thrown.
This page documents the use of the Error object itself and its use as a
constructor function. For a list of properties and methods inherited
by Error instances, see Error.prototype.
Error types
Besides the generic Error constructor, there are seven other core error
constructors in JavaScript. For client-side exceptions, see Exception
Handling Statements.
EvalError
Creates an instance representing an error that occurs regarding the
global function eval().
InternalError
Creates an instance representing an error that occurs when an internal
error in the JavaScript engine is thrown. E.g. "too much recursion".
RangeError
Creates an instance representing an error that occurs when a numeric
variable or parameter is outside of its valid range.
ReferenceError
Creates an instance representing an error that occurs when de-
referencing an invalid reference.
SyntaxError
Creates an instance representing a syntax error that occurs while
parsing code in eval().
TypeError
Creates an instance representing an error that occurs when a variable or
parameter is not of a valid type.
URIError
Creates an instance representing an error that occurs
when encodeURI() or decodeURI() are passed invalid parameters.
Properties
Error.prototype
Allows the addition of properties to Error instances.
Methods
The global Error object contains no methods of its own, however, it does
inherit some methods through the prototype chain.
Error instances
All Error instances and instances of non-generic errors inherit
from Error.prototype. As with all constructor functions, you can use the
prototype of the constructor to add properties or methods to all
instances created with that constructor.
Properties
Standard properties
Error.prototype.constructor
Specifies the function that created an instance's prototype.
Error.prototype.message
Error message.
Error.prototype.name
Error name.
Vendor-specific extensions
Non-standard
This feature is non-standard and is not on a standards track. Do not
use it on production sites facing the Web: it will not work for every
user. There may also be large incompatibilities between
implementations and the behavior may change in the future.
Microsoft
Error.prototype.description
Error description. Similar to message.
Error.prototype.number
Error number.
Mozilla
Error.prototype.fileName
Path to file that raised this error.
Error.prototype.lineNumber
Line number in file that raised this error.
Error.prototype.columnNumber
Column number in line that raised this error.
Error.prototype.stack
Stack trace.
Methods
Error.prototype.toSource()
Returns a string containing the source of the specified Error object;
you can use this value to create a new object. Overrides
the Object.prototype.toSource() method.
Error.prototype.toString()
Returns a string representing the specified object. Overrides
the Object.prototype.toString() method.
Examples
Throwing a generic error
Usually you create an Error object with the intention of raising it using
the throwkeyword. You can handle the error using
the try...catch construct:
try {
throw new Error('Whoops!');
} catch (e) {
console.log(e.name + ': ' + e.message);
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.log(e.foo); //baz
console.log(e.message); //bazMessage
console.log(e.stack); //stacktrace
}
ES5 Custom Error Object
All browsers include the CustomError constructor in the stacktrace when using
a prototypal declaration.
function CustomError(foo, message, fileName, lineNumber) {
var instance = new Error(message, fileName, lineNumber);
instance.foo = foo;
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
Error.captureStackTrace(instance, CustomError);
return instance;
}
CustomError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf){
Object.setPrototypeOf(CustomError, Error);
} else {
CustomError.__proto__ = Error;
}
try {
throw new CustomError('baz', 'bazMessage');
} catch(e){
console.log(e.foo); //baz
console.log(e.message) ;//bazMessage
}