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.

The following example demonstrates a try..catch statement. The code within the try<br />

block is executed. If an exception is thrown by any code within the try block, control passes<br />

to the catch block, which shows the error message in a text field using the<br />

Error.toString() method.<br />

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

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 />

trace("success");<br />

}<br />

catch (e) {<br />

this.createTextField("status_txt", this.getNextHighestDepth(), 0, 0, 100,<br />

22);<br />

status_txt.autoSize = true;<br />

status_txt.text = e.toString();<br />

}<br />

The following example shows a try code block with multiple, typed catch code blocks.<br />

Depending on the type of error that occurred, the try code block throws a different type of<br />

object. In this case, myRecordSet is an instance of a (hypothetical) class named RecordSet<br />

whose sortRows() method can throw two types of errors, RecordSetException <strong>and</strong><br />

MalformedRecord.<br />

In the following example, the RecordSetException <strong>and</strong> MalformedRecord objects are<br />

subclasses of the Error class. Each is defined in its own AS class file.<br />

// In RecordSetException.as:<br />

class RecordSetException extends Error {<br />

var message = "Record set exception occurred.";<br />

}<br />

// In MalformedRecord.as:<br />

class MalformedRecord extends Error {<br />

var message = "Malformed record exception occurred.";<br />

}<br />

Within the RecordSet class's sortRows() method, one of these previously defined error<br />

objects is thrown, depending on the type of exception that occurred. The following example<br />

shows how this code might look:<br />

class RecordSet {<br />

function sortRows() {<br />

var returnVal:Number = r<strong>and</strong>omNum();<br />

if (returnVal == 1) {<br />

204 ActionScript language elements

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

Saved successfully!

Ooh no, something went wrong!