Error

This article is in need of a technical review.

  • Tags
  • Files

Summary

Creates an error object.

Syntax

new Error([message[, fileName[, lineNumber]]])

Parameters

message
Human-readable description of the error
fileName Non-standard
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 Non-standard
The value for the lineNumber property on the created Error object. Defaults to the line number containing the Error() constructor invokation.

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. For client-side exceptions, see Exception Handling Statements.

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 available on Error instances, see Properties of Error instances.
prototype
Allows the addition of properties to Error instances.
Properties inherited from Function.prototype

Methods

For methods available on 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 Function.prototype

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.

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

this should probably be removed 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
}

Custom Error Types

You might want to define your own error types deriving from Error to be able to throw new MyError() and use instanceof MyError to check the kind of error in the exception handler. The common way to do this is demonstrated below.

Note that the thrown MyError will report incorrect lineNumber and fileName at least in Firefox.

See also the "What's a good way to extend Error in JavaScript?" discussion on Stackoverflow.

// Create a new object, that prototypally inherits from the Error constructor.
function MyError(message) {
  this.name = "MyError";
  this.message = message || "Default Message";
}
MyError.prototype = new Error();
MyError.prototype.constructor = MyError;

try {
  throw new MyError();
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "Default Message"
}

try {
  throw new MyError("custom message");
} catch (e) {
  console.log(e.name);     // "MyError"
  console.log(e.message);  // "custom message"
}

See also