The Error Subclasses
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError



Navigation Aids -- This Page        Navigation Aids -- This Topic        Navigation Aids -- This Site








Error Subclass Common Description

This page describes the six subclasses of the Error class. These subclasses are: EvalError, RangeError, ReferenceError, SyntaxError, TypeError and URIError.

An instance of a subclass is created whenever an exception is thrown by the host environment. In addition, we as programmers can create an error instance by invoking one of the six constructors for our subclasses.

The JavaScript Error class and the six subclasses are features available to programmers to assist with exception handling. This whole area of exception handling including the throw statement and the try/catch/finally statement has been addressed at Exception Handling in Client-Side Web Development.





Error Subclass Common Properties

The error subclasses all have the same two properties. These two properties are used in the same manner for all six error subclasses.

The error subclasses do inherit the constructor property from the base Object class.





Error Subclass Methods

The subclasses of the Error class do not own any explicit methods in of themselves. The subclasses do inherit from the Error super class. Recall that the Error super class has one method (toString). Moving up the inheritance chain, the subclasses of the Error class also inherit methods from the base Object class.





The EvalError SubClass


• Object Description:
The browser/interpreter can throw an instance of the EvalError when the function eval() was called illegally.
• Constructor Syntax:
var instanceName = new EvalError( message );
• Constructor Example:
var evalException = new EvalError("illegal use of eval function");
• Constructor Returns:
The constructor returns a new instance of the EvalError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new EvalError instance can also be created when the constructor is used as a function. var instanceName = EvalError( message );




The RangeError SubClass


• Object Description:
The browser/interpreter can throw an instance of the RangeError when a numeric value is not in its valid range.
• Constructor Syntax:
var instanceName = new RangeError( message );
• Constructor Example:
var rangeException = new RangeError("array length error");
• Constructor Returns:
The constructor returns a new instance of the RangeError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new RangeError instance can also be created when the constructor is used as a function. var instanceName = RangeError( message );




The ReferenceError SubClass


• Object Description:
The browser/interpreter can throw an instance of the ReferenceError when when you attempt to access a variable that does not exist.
• Constructor Syntax:
var instanceName = new ReferenceError( message );
• Constructor Example:
var refException = new ReferenceError("the variable does not exist");
• Constructor Returns:
The constructor returns a new instance of the ReferenceError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new ReferenceError instance can also be created when the constructor is used as a function. var instanceName = ReferenceError( message );




The SyntaxError SubClass


• Object Description:
The browser/interpreter can throw an instance of the SyntaxError when certain functions (RegExp(), Function(), eval()) experience syntax errors.
• Constructor Syntax:
var instanceName = new SyntaxError( message );
• Constructor Example:
var syntaxException = new SyntaxError("syntax error found in eval method");
• Constructor Returns:
The constructor returns a new instance of the SyntaxError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new SyntaxError instance can also be created when the constructor is used as a function. var instanceName = SyntaxError( message );




The TypeError SubClass


• Object Description:
The browser/interpreter can throw an instance of the TypeError when the value is not of the type required for the context. Accessing a property of null or undefined values may cause this error. Also, attempting to access a method that is not defined for the class can cause the error And, the error may occur when a function or method is called with too many arguments specified.
• Constructor Syntax:
var instanceName = new TypeError( message );
• Constructor Example:
var typeException = new TypeError("value is not number type");
• Constructor Returns:
The constructor returns a new instance of the TypeError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new TypeError instance can also be created when the constructor is used as a function. var instanceName = TypeError( message );




The URIError SubClass


• Object Description:
The browser/interpreter can throw an instance of the URIError when URI decoding functions ( decodeURI() and decodeURIComponent() ) incur decoding problems. The encoding functions ( encodeURI() and encodeURIComponent() ) can do the same.
• Constructor Syntax:
var instanceName = new URIError( message );
• Constructor Example:
var URIException = new URIError("decoding error");
• Constructor Returns:
The constructor returns a new instance of the URIError object. The constructor takes a single argument that will populate the message property. The constructor will use an implementation specific default message when the argument to the constructor is not supplied. A new URIError instance can also be created when the constructor is used as a function. var instanceName = URIError( message );




Error Subclass Constructor Examples

var evalException = new EvalError("illegal use of eval function"); var rangeException = new RangeError("array length error"); var refException = new ReferenceError("the variable does not exist"); var syntaxException = new SyntaxError("syntax error found in eval method"); var typeException = new TypeError("value is not number type"); var URIException = new URIError("decoding error");

Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson