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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example<br />

The following example shows how to create a try..finally statement. Because code in the<br />

finally block is guaranteed to execute, it is typically used to perform any necessary clean-up<br />

after a try block executes. In the following example, setInterval()calls a function every<br />

1000 millisecond (1 second). If an error occurs, an error is thrown <strong>and</strong> is caught by the catch<br />

block. The finally block is always executed whether or not an error occurs. Because<br />

setInterval() is used, clearInterval() must be placed in the finally block to ensure<br />

that the interval is cleared from memory.<br />

myFunction = function () {<br />

trace("this is myFunction");<br />

};<br />

try {<br />

myInterval = setInterval(this, "myFunction", 1000);<br />

throw new Error("my error");<br />

}<br />

catch (myError:Error) {<br />

trace("error caught: "+myError);<br />

}<br />

finally {<br />

clearInterval(myInterval);<br />

trace("error is cleared");<br />

}<br />

In the following example, the finally block is used to delete an ActionScript object,<br />

regardless of whether an error occurred. Create a new AS file called Account.as.<br />

class Account {<br />

var balance:Number = 1000;<br />

function getAccountInfo():Number {<br />

return (Math.round(Math.r<strong>and</strong>om() * 10) % 2);<br />

}<br />

}<br />

In the same directory as Account.as, create a new AS or FLA document <strong>and</strong> enter the<br />

following ActionScript in Frame 1 of the Timeline:<br />

import Account;<br />

var account:Account = new Account();<br />

try {<br />

var returnVal = account.getAccountInfo();<br />

if (returnVal != 0) {<br />

throw new Error("Error getting account information.");<br />

}<br />

}<br />

finally {<br />

if (account != null) {<br />

delete account;<br />

}<br />

}<br />

Statements 203

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

Saved successfully!

Ooh no, something went wrong!