Top Level

# Error - AS3

Package | Top Level
Class | public dynamic class Error
Inheritance | Error Object
Subclasses | ArgumentError, AugmentedError, AutomationError, CollectionViewError, ComplexCyclicDependencyError, Conflict, ConstraintError, CursorError, DataServiceError, DefinitionError, DRMManagerError, EvalError, ExpressionError, ExpressionEvaluationError, ExpressionValidationError, Fault, FormBridgeError, FunctionSequenceError, IllegalOperationError, InvalidCategoryError, InvalidFilterError, InvalidSWFError, IOError, ItemPendingError, MediaError, MemoryError, MessagingError, MetricError, NoDataAvailableError, PermissionError, PersistenceError, PersistenceError, ProxyServiceError, RangeError, ReferenceError, ScriptTimeoutError, SecurityError, SimpleCyclicDependencyError, SortError, SQLError, StackOverflowError, SyncManagerError, SyntaxError, TypeError, UnresolvedConflictsError, URIError, VerifyError, VideoError, VideoError

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

The Error class contains information about an error that occurred in a script. In developing ActionScript 3.0 applications, when you run your compiled code in the debugger version of a Flash runtime, a dialog box displays exceptions of type Error, or of a subclass, to help you troubleshoot the code. You create an Error object by using the `Error` constructor function. Typically, you throw a new Error object from within a `try` code block that is caught by a `catch` code block.

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

View the examples

More examples

Working with the debugger versions of Flash runtimes
Using try..catch..finally statements
Creating custom error classes
Responding to error events and status
Handling errors example: CustomErrors application

Learn more

Basics of error handling
Error handling in ActionScript 3.0
ActionScript 3.0 error-handling elements
Error-handling strategies
Comparing the Error classes
Core Error classes

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : Object A reference to the class object or constructor function for a given object instance. | Object
|  | errorID : int [read-only] Contains the reference number associated with the specific error message. | Error
|  | message : String Contains the message associated with the Error object. | Error
|  | name : String Contains the name of the Error object. | Error

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Error(message:String = "", id:int = 0) Creates a new Error object. | Error
|  | getStackTrace():String Returns the call stack for an error at the time of the error's construction as a string. | Error
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String [override] Returns the string "Error" by default or the value contained in the Error.message property, if defined. | Error
|  | valueOf():Object Returns the primitive value of the specified object. | Object

Property Detail

### errorID

property

`errorID:int` [read-only]

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

Contains the reference number associated with the specific error message. For a custom Error object, this number is the value from the `id` parameter supplied in the constructor.

Implementation
`public function get errorID():int`

### message

property

`public var message:String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

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.

Related API Elements

statements.html#throw
statements.html#try..catch..finally

### name

property

`public var name:String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

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

Related API Elements

statements.html#throw
statements.html#try..catch..finally
Constructor Detail

### Error

()
Constructor

`public function Error(message:String = "", id:int = 0)`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

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.
| `id:int` (default = `0`)`` — A reference number to associate with the specific error message.

Related API Elements

statements.html#throw
statements.html#try..catch..finally
Example ( How to use this example )
The following example creates a new Error object `err` and then, using the `Error()` constructor, assigns the string `"New Error Message"` to `err`.
Copy

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

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

Method Detail

### getStackTrace

()
method

`public function getStackTrace():String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

Returns the call stack for an error at the time of the error's construction as a string. As shown in the following example, the first line of the return value is the string representation of the exception object, followed by the stack trace elements:

Copy

         TypeError: Error #1009: Cannot access a property or method of a null object reference
             at com.xyz::OrderEntry/retrieveData()[/src/com/xyz/OrderEntry.as:995]
             at com.xyz::OrderEntry/init()[/src/com/xyz/OrderEntry.as:200]
             at com.xyz::OrderEntry()[/src/com/xyz/OrderEntry.as:148]

The preceding listing shows the value of this method when called in a debugger version of Flash Player or code running in the AIR Debug Launcher (ADL). When code runs in a release version of Flash Player or AIR, the stack trace is provided without the file path and line number information, as in the following example:

Copy

         TypeError: Error #1009: Cannot access a property or method of a null object reference
             at com.xyz::OrderEntry/retrieveData()
             at com.xyz::OrderEntry/init()
             at com.xyz::OrderEntry()

For Flash Player 11.4 and earlier and AIR 3.4 and earlier, stack traces are only available when code is running in the debugger version of Flash Player or the AIR Debug Launcher (ADL). In non-debugger versions of those runtimes, calling this method returns `null`.

Returns

| `String` — A string representation of the call stack.

### toString

()
method

`override public function toString():String`

Language Version: | ActionScript 3.0

Runtime Versions: | AIR 1.0, Flash Player 9, Flash Lite 4

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

Returns

| `String` — The error message.

Related API Elements

Error.message
statements.html#throw
statements.html#try..catch..finally
Example ( How to use this example )
The following example creates a new Error object `err` and then, using the `Error()` constructor, assigns the string `"New Error Message"` to `err`. Finally, the `message` property is set to `"Another New Error Message"`, which overwrites `"New Error Message"`.
Copy

    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

Examples How to use this example
ErrorExample.as
The following example uses the `ErrorExample` class to show how a custom error can be generated. This is accomplished with the following steps:

1. A local variable `nullArray` of Array type is declared, but notice that a new Array object is never created.
2. The constructor attempts to load a value into the uninitialized array by using the `push()` method within an error handling code segment that catches a custom error by using the `CustomError` class, which extends `Error`.
3. When the CustomError is thrown, the constructor catches it and then outputs an error message by using the `trace()` statement.

Copy

    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 function CustomError(message:String)
        {
            super(message);
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00