Change Language


Follow Navioo On Twitter

Error : Object

Represents a runtime error.

Platform Support

IE Mozilla Netscape Opera Safari
5.0+ 1.0+ 6.0+ 7.0+ 1.0+

Constructors

Constructor Action IE Mozilla Netscape Opera Safari
Error Constructor([String message]) : Error
Constructs a new instance of an Error object.
Show Details 5.0+ 1.0+ 6.0+ 7.0+ 1.0+

Error([String message]) : Error

Constructs a new instance of an Error object.

Parameters

String message (optional)Error message.

Returns

Error

Properties

Property Action IE Mozilla Netscape Opera Safari
constructor : Object
Specifies the function that creates the Error prototype.
Show Details 5.0+ 1.0+ 6.0+ 7.0+ 1.0+
Availability

JavaScript 1.5|JScript 5.0|ECMAScript v3

description : String
Description or message text of the error.
Show Details 5.0+ no no no no
Availability

JScript 5.0

fileName : String
Path or URL to the file that raised the error.
Show Details no 1.0+ 6.0+ no no
Availability

JavaScript 1.5

lineNumber : Number
Line number in file that raised the error.
Show Details no 1.0+ 6.0+ no no
Availability

JavaScript 1.5

message : String
Error message.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+
Availability

JavaScript 1.5|JScript 5.5|ECMAScript v3

name : String
Error name.
Show Details 5.5+ 1.0+ 6.0+ 7.0+ 1.0+
Availability

JavaScript 1.5|JScript 5.5|ECMAScript v3

number : Number
Error number.
Show Details 5.0+ no no no no
Availability

JScript 5.0

prototype : Object
Reference to the Error object prototype.
Show Details 5.0+ 1.0+ 6.0+ 7.0+ 1.0+
Availability

JavaScript 1.5|JScript 5.0|ECMAScript v3

stack : String
Stack trace that gives information about the context of the error.
Show Details 5.0+ 1.0+ 6.0+ no no
Availability

JavaScript 1.5

Functions

Method Action IE Mozilla Netscape Opera Safari
toString() : String
Converts an Error object to a string.
Show Details 5.0+ 1.0+ 6.0+ 7.0+ 1.0+

Returns

String
Availability

JavaScript 1.5|JScript 5.5|ECMAScript v3

Throwing a generic error

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);
         }

Handling a specific error

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's name 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);
         }
         }

Remarks

Besides the base Error, there are six other core error types in JavaScript 1.5:

  • EvalError: raised when an error occurs executing code in eval()
  • RangeError: raised when a numeric variable or parameter is outside of its valid range
  • ReferenceError: raised when de-referencing an invalid reference
  • SyntaxError: raised when a syntax error occurs while parsing code in eval()
  • TypeError: raised when a variable or parameter is not a valid type
  • URIError: raised when encodeURI() or decodeURI() are passed invalid parameters

References

EvalError|RangeError|ReferenceError|SyntaxError|TypeError|URIError

Availability

JavaScript 1.5|JScript 5.5|ECMAScript v3

text_javascript Navioo_docs

Examples -> Source code Demo online - > error:

Ajax Javascript feed

↑ Grab this Headline Animator