03.05.2013 Views

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

FLASH® LITE™ 2.x - Adobe Help and Support

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example<br />

In the following example, a function throws a specified message depending on the parameters<br />

entered into theNum. If two numbers can be divided, SUCCESS <strong>and</strong> the number are shown.<br />

Specific errors are shown if you try to divide by 0 or enter only 1 parameter:<br />

function divideNum(num1:Number, num2:Number):Number {<br />

if (isNaN(num1) || isNaN(num2)) {<br />

throw new Error("divideNum function requires two numeric parameters.");<br />

} else if (num2 == 0) {<br />

throw new Error("cannot divide by zero.");<br />

}<br />

return num1/num2;<br />

}<br />

try {<br />

var theNum:Number = divideNum(1, 0);<br />

trace("SUCCESS! "+theNum);<br />

} catch (e_err:Error) {<br />

trace("ERROR! "+e_err.message);<br />

trace("\t"+e_err.name);<br />

}<br />

If you test this ActionScript without any modifications to the numbers you divide, you see an<br />

error displayed in the Output panel because you are trying to divide by 0.<br />

See also<br />

throw statement, try..catch..finally statement<br />

name (Error.name property)<br />

public name : String<br />

Contains the name of the Error object. By default, the value of this property is "Error".<br />

Example<br />

In the following example, a function throws a specified error depending on the two numbers<br />

that you try to divide. Add the following ActionScript to Frame 1 of the Timeline:<br />

function divideNumber(numerator:Number, denominator:Number):Number {<br />

if (isNaN(numerator) || isNaN(denominator)) {<br />

throw new Error("divideNum function requires two numeric parameters.");<br />

} else if (denominator == 0) {<br />

throw new DivideByZeroError();<br />

}<br />

return numerator/denominator;<br />

}<br />

try {<br />

var theNum:Number = divideNumber(1, 0);<br />

trace("SUCCESS! "+theNum);<br />

Error 335

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!