

| IE | Mozilla | Netscape | Opera | Safari | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
|---|
| Constructor | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Constructs a new instance of an Error object. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
| Property | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Specifies the function that creates the Error prototype. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Description or message text of the error. | 5.0+ | no | no | no | no |
Path or URL to the file that raised the error. | no | 1.0+ | 6.0+ | no | no |
Line number in file that raised the error. | no | 1.0+ | 6.0+ | no | no |
Error message. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Error name. | 5.5+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Error number. | 5.0+ | no | no | no | no |
Reference to the Error object prototype. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
Stack trace that gives information about the context of the error. | 5.0+ | 1.0+ | 6.0+ | no | no |
| Method | IE | Mozilla | Netscape | Opera | Safari |
|---|---|---|---|---|---|
Converts an Error object to a string. | 5.0+ | 1.0+ | 6.0+ | 7.0+ | 1.0+ |
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);}You can choose to handle only specific error types by testing the error type with the 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}You can also test the error'sname property to determine the error type. You can use this facility to create "user-defined" error types:
try { var error = new Error("Whoops!"); error.name = "MyError"; throw error;} catch (e) { if (e.name == "EvalError") { alert(e.name + ": " + e.message); } else if (e.name == "RangeError") { alert(e.name + ": " + e.message); } // ... etc else if (e.name == "MyError") { alert(e.name + ": " + e.message); }}Besides the baseError, there are six other core error types in JavaScript 1.5:
eval()eval()encodeURI() ordecodeURI() are passed invalid parametersEvalError | RangeError | ReferenceError | SyntaxError | TypeError | URIError
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
Constructs a new instance of an Error object.
| String | message | Error message. (optional) |
Specifies the function that creates the Error prototype.
JavaScript 1.5 | JScript 5.0 | ECMAScript v3
Description or message text of the error.
JScript 5.0
Path or URL to the file that raised the error.
JavaScript 1.5
Line number in file that raised the error.
JavaScript 1.5
Error message.
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
Error name.
JavaScript 1.5 | JScript 5.5 | ECMAScript v3
Error number.
JScript 5.0
Reference to the Error object prototype.
JavaScript 1.5 | JScript 5.0 | ECMAScript v3
Stack trace that gives information about the context of the error.
JavaScript 1.5