JavaScript tutorial:
Error object

 

Properties: description Property, number Property

The Error object contains information about errors.

Syntax

var newErrorObj = new Error()
var newErrorObj = new Error(number)
var newErrorObj = new Error(number, description)

The Error object constructor syntax has these parts:

Part

Description

number

Numeric value assigned to an error. Zero if omitted.

description

Brief string that describes an error. Empty string if omitted.

Example

Whenever a run-time error occurs, an instance of the Error object is created to describe the error. This instance has two intrinsic properties that contain the description of the error (description property) and the error number (number property).

An error number is a 32-bit value. The upper 16-bit word is the facility code, while the lower word is the actual error code.

Error objects can also be explicitly created, using the syntax shown above, or thrown using the throw statement. In both cases, you can add any properties you choose, to expand the capability of the Error object.

Typically, the local variable that's created in a try...catch statement refers to the implicitly created Error object. As a result, you can use the error number and description in any way you choose.

The following example illustrates the use of the implicitly created Error object:

<HTML>
<HEAD>
<SCRIPT LANGUAGE= JavaScript>
try
{
x = y // Cause an error.
}
catch(e)
{ // Create local variable e.
alert(e) // Prints "[object Error]".
alert(e.number & 0xFFFF) // Prints 5009.
alert(e.description) // Prints "'y' is undefined".
}
</SCRIPT>
</HEAD>

  To run the code above, paste it into JavaScript Editor, and go to View, Internal Page Viewer or press F5.

See also: new Operator, throw Statement, try...catch Statement, var Statement