Error


Object   |   +-Error public class Error extends Object

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.

Availability: ActionScript 1.0; Flash Player 7

Property summary

Modifiers

Property

Description

 

message:String

Contains the message associated with the Error object.

 

name:String

Contains the name of the Error object.


Properties inherited from class Object

constructor (Object.constructor property), __proto__ (Object.__proto__ property), prototype (Object.prototype property), __resolve (Object.__resolve property)


Constructor summary

Signature

Description

Error([message:String])

Creates a new Error object.


Method summary

Modifiers

Signature

Description

 

toString() : String

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


Methods inherited from class Object

addProperty (Object.addProperty method), hasOwnProperty (Object.hasOwnProperty method), isPropertyEnumerable (Object.isPropertyEnumerable method), isPrototypeOf (Object.isPrototypeOf method), registerClass (Object.registerClass method), toString (Object.toString method), unwatch (Object.unwatch method), valueOf (Object.valueOf method), watch (Object.watch method)


Error constructor

public Error([message:String])

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

Availability: ActionScript 1.0; Flash Player 7

Parameters

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

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

See also

throw statement, try..catch..finally statement

message (Error.message property)

public 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.

Availability: ActionScript 1.0; Flash Player 7

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.

See also

throw statement, try..catch..finally statement

name (Error.name property)

public name : String

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

Availability: ActionScript 1.0; Flash Player 7

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."; }

See also

throw statement, try..catch..finally statement

toString (Error.toString method)

public toString() : String

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

Availability: ActionScript 1.0; Flash Player 7

Returns

String - A String

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

See also

message (Error.message property), throw statement, try..catch..finally statement



ActionScript 2.0 Language Reference for Macromedia Flash 8
ActionScript 2.0 Language Reference for Macromedia Flash 8
ISBN: 0321384040
EAN: 2147483647
Year: 2004
Pages: 113

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net