This object represents exceptions, and is often used for describing some error that has occured. The Error object itself is primarily an implementation base-class that is intended to be extended to implement specific error messages. For a list of the native objects that ECMAScript uses, see the following table.
| Object | Purpose |
|---|---|
| EvalError | When the eval global method is used incorrectly, this error type is thrown. |
| RangeError | Whenever a numeric value has been supplied to a method that exceeds the acceptable range for that property, this error is returned. |
| ReferenceError | This error is returned when an invalid reference value is used. |
| SyntaxError | When some form of parsing error occurs, for instance in RegExp, this error is returned. |
| TypeError | Many methods are flexible in the datatypes they can accept as parameters. However, when a method is given a datatype it cannot properly operate on, it may return this error. |
| URIError | This error is returned when one of the global URI handling functions is used incorrectly. |
Extending the Error object for your own purposes is quite straight-forward. One simply inherits from the Error object (using its prototype property), and overrides the message and name properties. For example:
Custom Error implementation examplevar EmailError = Error.constructor; EmailError.prototype.message = function() { return "There was an error in validating the supplied email address"; }; EmailError.prototype.name = function() { return "EmailError"; };
When creating your own objects intended for use within your own, or other, applications, it is a good idea to create your own error objects to indicate error statuses. This works much comfortably within a ECMAScript
try /catch block.
throw new Error("Unidentified widget");Initial message for this error.
This is the constructor for creating new Error objects. This can be used in creating generic errors for which the default error-specific objects do not apply, or for creating your own Error objects. The message parameter, if supplied, will be used as the message for this error, which can be conveyed to the user or some other aspect of your application.
When Error is called as a function, it is functionally equivilant to creating it with the new constructor.
try {
// Do something dangerous
} catch (e) {
window.alert("Some error occurred: " + e.message);
}This property contains a full string description of the error that has occurred. When creating custom Error objects, this is one of the methods that is often overridden.
function errorHandler(e) {
if (e.name == "URIError") {
// An error occured when working with a URI
}
}This property returns the name of this error type, which is usually the same as the name of the object. This is often used by programs to inspect an Error object to determine it's type. When creating custom Error objects, this is one of the methods that is often overridden.

Copyright © 2004 Michael A Nachbaur; All Rights Reserved
