PackageTop Level
Classpublic dynamic class Error
InheritanceError Inheritance Object
SubclassesArgumentError, DefinitionError, EvalError, IllegalOperationError, InvalidSWFError, IOError, MemoryError, RangeError, ReferenceError, ScriptTimeoutError, SecurityError, StackOverflowError, SyntaxError, TypeError, URIError, VerifyError

Contains information about an error that occurred in a script. While developing ActionScript 3.0 applications, you can run your compiled code and Flash Player will display exceptions of type Error, or of a subclass, in a dialog box at runtime to help you troubleshoot the code. You create an Error object using the Error constructor function. Typically, you throw a new Error object from within a try code block that is then caught by a catch or finally code block.

You can also create a subclass of the Error class and throw instances of that subclass.

View the examples.

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties
 PropertyDefined by
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  message : String
Contains the message associated with the Error object.
Error
  name : String
Contains the name of the Error object.
Error
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods
 FunctionDefined by
  
Error(message:String = "")
Creates a new Error object.
Error
  
For debugger versions of the Flash Player, only; this method returns the call stack for an error as a string at the time of the error's construction.
Error
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
Returns the string "Error" by default or the value contained in Error.message, if defined.
Error
 Inherited
Returns the primitive value of the specified object.
Object
Property detail
message property
public var message:String

Contains the message associated with the Error object. By default, the value of this property is "Error". You can specify a message property when you create an Error object by passing the error string to the Error constructor function.

See also
statements.html#throw, statements.html#try..catch..finally
name property
public var name:String

Contains the name of the Error object. By default, the value of this property is "Error".

See also
statements.html#throw, statements.html#try..catch..finally
Constructor detail
Error constructor

public function Error(message:String = "")

Creates a new Error object. If message is specified, its value is assigned to the object's Error.message property.

Parameters
message:String (default = "") — A string associated with the Error object; this parameter is optional.

Example
The following example creates a new Error object err and then, using the Error() constructor, assigns the string New Error Message to err.

var err:Error = new Error();
trace(err.toString());    // Error

err = new Error("New Error Message");
trace(err.toString());    // Error: New Error Message

See also
statements.html#throw, statements.html#try..catch..finally
Method detail
getStackTrace method

public function getStackTrace():String

For debugger versions of the Flash Player, only; this method returns the call stack for an error as a string at the time of the error's construction. The first line of the return value is the string representation of the exception object, followed by the strack trace elements. For example:
  TypeError: null cannot be converted to an object
      at com.xyz.OrderEntry.retrieveData(OrderEntry.as:995)
      at com.xyz.OrderEntry.init(OrderEntry.as:200)
      at com.xyz.OrderEntry.$construct(OrderEntry.as:148)
     

Returns
String — A string representation of the call stack.
toString method

public override function toString():String

Returns the string "Error" by default or the value contained in Error.message, if defined.

Returns
String — The error message.

Example
The following example creates a new Error object err and then, using the Error() constructor, assigns the string New Error Message to err. Lastly the message property is set to Another New Error Message, which overwrites New Error Message.

Notice how the toString() method is explicitally called within each trace() statement. Although this is not normally required, it is recommended regardless.


var err:Error = new Error();
trace(err.toString());    // Error

err = new Error("New Error Message");
trace(err.toString());    // Error: New Error Message

err.message = "Another New Error Message";
trace(err.toString());    // Error: Another New Error Message

See also
Error.message, statements.html#throw, statements.html#try..catch..finally
Class examples

The following example uses the class ErrorExample to show how a custom error can be generated. This is accomplished using the following steps:
  1. A local variable nullArray of type Array is declared, but notice a new Array object is never created.
  2. The constructor then attempts to load a value into the un-initialized array using the push() method within an error handling code segment that catches a custom error using the CustomError class, which extends Error.
  3. After the CustomError is thrown, the constructor catches it and then outputs an error message via the trace statement.
package {

    import flash.display.Sprite;

    public class ErrorExample extends Sprite {
        private var nullArray:Array;

        public function ErrorExample() {
            try {
                nullArray.push("item");
            }
            catch(e:Error) {
                throw new CustomError("nullArray is null");
            }

        }
    }
}

class CustomError extends Error {
    public var name:String = "CustomError";

    public function CustomError(message:String) {
        super(message);
    }
}