360
ActionScript classes
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 Lite 2.0
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 {
Содержание Flash Lite 2
Страница 1: ...Flash Lite 2 x ActionScript Language Reference...
Страница 22: ...22 Contents...
Страница 244: ...244 ActionScript language elements...
Страница 760: ...760 ActionScript classes...