| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | f1_keywords | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Error Object (JavaScript) | Microsoft Docs |
01/18/2017 |
windows-client-threshold |
|
language-reference |
|
|
|
0b27d6ec-3997-4e91-a6c0-5afbaf494db7 |
25 |
mikejo5000 |
mikejo |
ghogen |
Contains information about errors.
errorObj = new Error()
errorObj = new Error([number])
errorObj = new Error([number[, description]])
errorObj
Required. The variable name to which the Error object is assigned. The variable assignment is omitted when you create the error using a throw statement.
number
Optional. Numeric value assigned to an error. Zero if omitted.
description
Optional. Brief string that describes an error. Empty string if omitted.
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). For more information, see JavaScript Run-time Errors.
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 is 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 Error object.
function checkInput(x) {
try
{
if (isNaN(parseInt(x))) {
throw new Error("Input is not a number.");
}
}
catch(e)
{
document.write(e.description);
}
}
checkInput("not a number");
The following example illustrates the use of the implicitly created Error object.
try
{
// Cause an error.
x = y;
}
catch(e)
{
document.write(e);
document.write ("<br />");
document.write ("Number: ");
document.write (e.number & 0xFFFF);
document.write ("<br />");
document.write ("Facility Code: ");
document.write(e.number>>16 & 0x1FFF);
document.write ("<br />");
document.write ("Description: ");
document.write (e.description);
}
// Output:
// ReferenceError: 'y' is undefined
// Number: 5009
// Facility Code: 10
// Description: 'y' is undefined
toString Method (Error) | valueOf Method (Date)
constructor Property (Error) | description Property | message Property | name Property | number Property | prototype Property (Error) | stack Property (Error) | stackTraceLimit Property (Error)
[!INCLUDEjsv5]
new Operator
throw Statement
try...catch...finally Statement
var Statement
Hilo JavaScript sample app (Windows Store)