EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Page 737

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
try..catch..finally 737
Example
The following example shows how to create a try..finally statement. Because code in the
finally block is guaranteed to execute, it is typically used to perform any necessary “clean-up
code after a
try block executes. In this example, the finally block is used to delete an
ActionScript object, regardless of whether an error occurred.
var account = new Account()
try {
var returnVal = account.getAccountInfo();
if(returnVal != 0) {
throw new Error("Error getting account information.");
}
}
finally {
// Delete the 'account' object no matter what.
if(account != null) {
delete account;
}
}
The following example demonstrates a try..catch statement. The code within the try block is
executed. If an exception is thrown by any code within the
try block, control passes to the catch
block, which displays the error message in a text field using the
Error.toString() method.
var account = new Account()
try {
var returnVal = account.getAccountInfo();
if(returnVal != 0) {
throw new Error("Error getting account information.");
}
} catch (e) {
status_txt.text = e.toString();
}
The following example shows a try code block with multiple, typed catch code blocks.
Depending on the type of error that occurred, the
try code block throws a different type of
object. In this case,
myRecordSet is an instance of a (hypothetical) class named RecordSet whose
sortRows() method can throw two different types of errors: RecordSetException and
MalformedRecord.
In this example, the RecordSetException and MalformedRecord objects are subclasses of the
Error class. Each is defined in its own AS class file. (For more information, see Chapter 9,
“Creating Classes with ActionScript 2.0,” on page 155.)
// In RecordSetException.as:
class RecordSetException extends Error {
var message = "Record set exception occurred."
}
// In MalformedRecord.as:
class MalformedRecord extends Error {
var message = "Malformed record exception occurred.";
}

Table of Contents

Related product manuals