加载中...

Mozilla Object Reference

Error

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.

Table 1: Native error objects defined in ECMA-262
ObjectPurpose
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 example
var 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.

See Also:
EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError

Constructor

Error([message]) : Error

Synopsis:
throw new Error("Unidentified widget");
Return Type:
Error
Parameters:
message : String

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.

See Also:
Error.message

Properties

message : String

Synopsis:
try {
  // Do something dangerous
} catch (e) {
  window.alert("Some error occurred: " + e.message);
}
Return Type:
String

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.

name : String

Synopsis:
function errorHandler(e) {
  if (e.name == "URIError") {
    // An error occured when working with a URI
  }
}
Return Type:
String

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.

Methods

toString() : String

Overrides:
Object.toString()
Return Type:
String

This method returns a human-readable string representation of this error. Though the result is implementation-dependant, Mozilla prints a string comprising both the name and, if not undefined, the message properties.

See Also:
Error.name, Error.message
Get Firefox!

Copyright © 2004 Michael A Nachbaur; All Rights Reserved