You are here: Reference > JavaScript > core > objects > Error

Error object

Browser support:
Represents a run-time error caused by a script operation.
JavaScript interpreters throw an Error object, when a script error (exception) occurs. In some cases when the error is caused by a DOM operation, JavaScript interpreters throw an DOMException object, not an Error object. The DOMException object is not supported in Internet Explorer before version 9, errors are always represented by Error objects in those browsers.

The Error and DOMException objects may be caught with the try...catch statement, and contain information about the occurred error.

The Error object can also be used to create an error message with the throw statement. In this case the Error object can be thrown as an exception.

Syntax:

Creating a new Error object:
Internet Explorer supports the following form:
var errorObj = new Error([number[, description]]);
number Optional. Integer that specifies the error code. Default is 0. Sets the error property of the Error object.
message Optional. String that specifies the message of the error. Default is an empty string. Sets the description and message properties of the Error object.
Firefox supports the following form:
var errorObj = new Error([message[, fileName[, lineNumber]]]);
message Optional. String that specifies the message of the error. Default is an empty string. Sets the description and message properties of the Error object.
fileName Optional. String that specifies the name of the file where the error occurs. Default is the file where the Error object is created. Sets the fileName property of the Error object.
lineNumber Optional. Integer that specifies the line number where the error occurs. Default is the line number, where the Error object is created. Sets the lineNumber property of the Error object.
Opera, Google Chrome and Safari support the following form:
var errorObj = new Error([message]);
message Optional. String that specifies the message of the error. Default is an empty string. Sets the description and message properties of the Error object.

Members:

The Error object inherits from the Error.prototype and Function.prototype objects. The following lists only contain the members of the Error and Error.prototype objects.

Properties:

Property Support Description
description*
Sets or retrieves the description of the error.
fileName*
Sets or retrieves the name of the file where the error occurred.
lineNumber*
Sets or retrieves the number of line where the error occurred.
message*
Sets or retrieves the error message as a string.
name*
Sets or retrieves the name of the error.
number*
Sets or retrieves the error code as a number.
stack*
Returns detailed information about the location where the error exactly occurred during the execution of the script.
prototype
Returns a reference to the Error.prototype object. The Error.prototype object allows adding properties and methods to the Error object that can be used with instances of the Error object, like any predefined property or method. The prototype property is static, it cannot be accessed from an instance of the Error object, only Error.prototype is allowed.

(*) - The property is inherited from the Error.prototype.

Methods:

Method Support Description
toSource ( )*
Returns a string representing the source code of the current Error object.
toString ( )*
Returns a string representing the value of the current Error object.
When an Error object needs to be converted to a string, the JavaScript interpreter automatically calls its toString method.

(*) - The method is inherited from the Error.prototype.

Examples:

Example 1:

This example shows how to catch an exception and displays information about the error:
try {
    unknownFunction ();
}
catch(e) {
    document.write ("The error message: <b>" + e.message + "</b>");
    if ('number' in e) {
        document.write ("<br />");
        document.write ("The error code: <b>" + e.number + "</b>");
    }
    if ('lineNumber' in e) {
        document.write ("<br />");
        document.write ("The error occurred at line: <b>" + e.lineNumber + "</b>");
    }
}
Did you find this example helpful? yes no

Example 2:

This example shows how to create and catch a custom exception:
try {
    var err = new Error ();
    err.message = "My first error message";
    if (err.fileName === undefined) { // Internet Explorer, Opera, Google Chrome and Safari
        err.fileName = document.location.href;
    }
    throw err;
}
catch(e) {
    document.write ("The error message: <b>" + e.message + "</b><br />");
    document.write ("The error occurred in the following file: <b>" + e.fileName + "</b>");
}
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content