Core JavaScript 1.5 Reference:Global Objects:Error
From MDC
Contents |
Summary
Creates an error object.
Syntax
new Error([message[, fileName[, lineNumber]]])
Parameters
-
message - Human-readable description of the error
-
fileName - Non-standard
- The name of the file containing the code that caused the exception
-
lineNumber - Non-standard
- The line number of the code that caused the exception
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 six other core error constructors in JavaScript:
- EvalError
- Creates an instance representing an error that occurs regarding the global function eval()
- 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
For properties inherited by Error instances, see Properties of Error instances.
- prototype
- Allows the addition of properties to
Errorinstances.
Properties inherited from Function.prototype
caller, constructor, length, name
Methods
For methods inherited by Error instances, see Methods of Error instances.
The global Error object contains no methods of its own, however, it does inherit some methods through the prototype chain.
Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, watch
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
- constructor
- Specifies the function that created an instance's prototype.
- message
- Error message.
- name
- Error name.
Vendor-specific extensions
Non-standard
Microsoft
- description
- Error description. Similar to message.
- number
- Error number.
Mozilla
- fileName
- Path to file that raised this error.
- lineNumber
- Line number in file that raised this error.
- stack
- Stack trace.
Methods
- toSource
- Non-standard
- Returns a string containing the source of the specified
Errorobject; you can use this value to create a new object. Overrides the Object.toSource method.
- toString
- Returns a string representing the specified object. Overrides the Object.toString method.
Methods inherited from Object.prototype
__defineGetter__, __defineSetter__, hasOwnProperty, isPrototypeOf, __lookupGetter__, __lookupSetter__, __noSuchMethod__, propertyIsEnumerable, unwatch, valueOf, watch
Examples
Example: Throwing a generic error
Usually you create an Error object with the intention of raising it using the throw keyword. You can handle the error using the try...catch construct:
try {
throw new Error("Whoops!");
} catch (e) {
alert(e.name + ": " + e.message);
}
Example: Handling a specific error
You can choose to handle only specific error types by testing the error type with the error's constructor property or, if you're writing for modern JavaScript engines, instanceof keyword:
try {
foo.bar();
} catch (e) {
if (e instanceof EvalError) {
alert(e.name + ": " + e.message);
} else if (e instanceof RangeError) {
alert(e.name + ": " + e.message);
}
// ... etc
}