Error Object

Contains information about errors. There are two forms of the Error constructor.

function Error([description : String ])
function Error([number : Number [, description : String ]])

Arguments

  • number
    Optional. Numeric value assigned to the error, specifying the value of the number property. Zero if omitted.

  • description
    Optional. Brief string that describes the error, specifying the initial value of the description and message properties. Empty string if omitted.

Remarks

Error objects can be explicitly created using the constructor shown above. You can add properties to the Error object to expand its capabilities. An Error object is also created whenever a run-time error occurs to describe the error.

Typically, an Error object is thrown with the throw statement and the expectation that it will be caught by a try...catch statement. You can use a throw statement to pass any type of data as an error; the throw statement will not implicitly create an Error object. However, by throwing an Error object, a catch block can treat JScript run-time errors and user-defined errors similarly.

The Error object has four intrinsic properties: the description of the error (description and message properties), the error number (number property), and the name of the error (name property). The description and message properties refer to the same message; the description property provides backwards compatibility, while the message property complies with the ECMA standard.

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. To read off the actual error code, use the & (bitwise And) operator to combine the number property with the hexadecimal number 0xFFFF.

Warning

Attempting to use the JScript Error object in an ASP.NET page may product unintended results. This results from the potential ambiguity between the JScript Error object and the Error event of the ASP.NET page. Use the System.Exception class instead of the Error object for error handling in ASP.NET pages.

Note

Only JScript provides the Error object. Since it is not derived from a .NET Framework type, other Common Language Specification (CLS) languages cannot use it. Consequently, when type-annotating the parameters and return types of CLS-compliant methods, make sure to use the System.Exception data type instead of the Error object. However, you may use the Error object to type annotate identifiers other than parameters or return types. For more information, see Writing CLS-Compliant Code.

Example

The following example illustrates a use of the Error object.

try {
   // Throw an error.
   throw new Error(42,"No question");
} catch(e) {
   print(e)
// Extract the error code from the error number.
   print(e.number & 0xFFFF)
   print(e.description)
}

The output of this code is:

Error: No question
42
No question

Properties and Methods

Error Object Properties and Methods

Requirements

Version 5

See Also

Reference

new Operator

throw Statement

try...catch...finally Statement

var Statement

Page