PackageTop Level
Classpublic class Error
InheritanceError Inheritance Object

Player version: Flash Player 7

Contains information about an error that occurred in a script. 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.



Public Properties
 Property
  message : String
Contains the message associated with the Error object.
  name : String
Contains the name of the Error object.
 Properties inherited from class Object
 __proto__, __resolve, constructor, prototype
Public Methods
 Method
  
Error([message:String])
Creates a new Error object.
  
Returns the string "Error" by default or the value contained in Error.message, if defined.
 Methods inherited from class Object
 addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch
Property detail
messageproperty
public var message:String

Player version: Flash Player 7

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


Example
In the following example, a function throws a specified message depending on the parameters entered into theNum. If two numbers can be divided, SUCCESS and the number are shown. Specific errors are shown if you try to divide by 0 or enter only 1 parameter:
function divideNum(num1:Number, num2:Number):Number {
    if (isNaN(num1) || isNaN(num2)) {
    throw new Error("divideNum function requires two numeric parameters.");
    } else if (num2 == 0) {
    throw new Error("cannot divide by zero.");
    }
    return num1/num2;
}
try {
    var theNum:Number = divideNum(1, 0);
    trace("SUCCESS! "+theNum);
} catch (e_err:Error) {
    trace("ERROR! "+e_err.message);
    trace("\t"+e_err.name);
}
If you test this ActionScript without any modifications to the numbers you divide, you see an error displayed in the Output panel because you are trying to divide by 0.

nameproperty 
public var name:String

Player version: Flash Player 7

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

See also


Example
In the following example, a function throws a specified error depending on the two numbers that you try to divide. Add the following ActionScript to Frame 1 of the Timeline:
function divideNumber(numerator:Number, denominator:Number):Number {
    if (isNaN(numerator) || isNaN(denominator)) {
    throw new Error("divideNum function requires two numeric parameters.");
    } else if (denominator == 0) {
    throw new DivideByZeroError();
    }
    return numerator/denominator;
}
try {
    var theNum:Number = divideNumber(1, 0);
    trace("SUCCESS! "+theNum);
    // output: DivideByZeroError -> Unable to divide by zero.
} catch (e_err:DivideByZeroError) {
    // divide by zero error occurred
    trace(e_err.name+" -> "+e_err.toString());
} catch (e_err:Error) {
    // generic error occurred
    trace(e_err.name+" -> "+e_err.toString());
}

To add a custom error, add the following code to a .AS file called DivideByZeroError.as and save the class file in the same directory as your FLA document.

class DivideByZeroError extends Error {
    var name:String = "DivideByZeroError";
    var message:String = "Unable to divide by zero.";
}

Constructor detail
Error()constructor
public function Error([message:String])

Player version: Flash Player 7

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

Parameters
message:String [optional] — A string associated with the Error object.

See also


Example
In the following example, a function throws an error (with a specified message) if the two strings that are passed to it are not identical:
function compareStrings(str1_str:String, str2_str:String):Void {
    if (str1_str != str2_str) {
    throw new Error("Strings do not match.");
    }
}
try {
    compareStrings("Dog", "dog");
    // output: Strings do not match.
} catch (e_err:Error) {
    trace(e_err.toString());
}

Method detail
toString()method
public function toString():String

Player version: Flash Player 7

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

Returns
String — A String

See also


Example
In the following example, a function throws an error (with a specified message) if the two strings that are passed to it are not identical:
function compareStrings(str1_str:String, str2_str:String):Void {
if (str1_str != str2_str) {
    throw new Error("Strings do not match.");
    }
}
try {
    compareStrings("Dog", "dog");
    // output: Strings do not match.
} catch (e_err:Error) {
    trace(e_err.toString());
}