The following example demonstrates a
executed. If an exception is thrown by any code within the
block, which shows the error message in a text field using the
In the same directory as Account.as, create a new FLA document and enter the following
ActionScript:
import Account;
var account:Account = new Account();
try {
var returnVal = account.getAccountInfo();
if (returnVal != 0) {
throw new Error("Error getting account information.");
}
trace("success");
} catch (e) {
this.createTextField("status_txt", this.getNextHighestDepth(), 0, 0, 100,
22);
status_txt.autoSize = true;
status_txt.text = e.toString();
}
The following example shows a
Depending on the type of error that occurred, the
object. In this case,
myRecordSet
method can throw two types of errors, RecordSetException and MalformedRecord.
sortRows()
In the following 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
"Creating Custom Classes with ActionScript 2.0," on page
// 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.";
}
Within the RecordSet class's
thrown, depending on the type of exception that occurred. The following example shows how
this code might look:
class RecordSet {
function sortRows() {
var returnVal:Number = randomNum();
if (returnVal == 1) {
throw new RecordSetException();
} else if (returnVal == 2) {
throw new MalformedRecord();
}
}
try..catch
code block with multiple, typed
try
is an instance of a (hypothetical) class named RecordSet whose
method, one of these previously defined error objects is
sortRows()
statement. The code within the
block, control passes to the
try
Error.toString()
catch
code block throws a different type of
try
45.)
block is
try
catch
method.
code blocks.
Chapter 2,
try..catch..finally
221
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?