Example
In the following example, a function throws a specified message depending on the parameters
entered into
. If two numbers can be divided,
theNum
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 "
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 {
360
ActionScript classes
and the number are shown.
SUCCESS
".
Error