Flash Lite 2 |
|||
| Flash Lite 2.x ActionScript Language Reference > ActionScript language elements > Statements > try..catch..finally statement | |||
try {// ... try block ... } finally { // ... finally block ... }
try { // ... try block ... }
catch(error [:ErrorType1]) // ... catch block ... }
[catch(error[:ErrorTypeN]) { // ... catch block ... }]
[finally { // ... finally block ... }]
Enclose a block of code in which an error can occur, and then respond to the error. If any code within the try code block throws an error (using the throw statement), control passes to the catch block, if one exists, and then to the finally code block, if one exists. The finally block always executes, regardless of whether an error was thrown. If code within the try block doesn't throw an error (that is, if the try block completes normally), then the code in the finally block is still executed. The finally block executes even if the try block exits using a return statement.
A try block must be followed by a catch block, a finally block, or both. A single try block can have multiple catch blocks but only one finally block. You can nest try blocks as many levels deep as desired.
The error parameter specified in a catch handler must be a simple identifier such as e or theException or x. The variable in a catch handler can also be typed. When used with multiple catch blocks, typed errors let you catch multiple types of errors thrown from a single try block.
If the exception thrown is an object, the type will match if the thrown object is a subclass of the specified type. If an error of a specific type is thrown, the catch block that handles the corresponding error is executed. If an exception that is not of the specified type is thrown, the catch block does not execute and the exception is automatically thrown out of the try block to a catch handler that matches it.
If an error is thrown within a function, and the function does not include a catch handler, then the ActionScript interpreter exits that function, as well as any caller functions, until a catch block is found. During this process, finally handlers are called at all levels.
Availability: ActionScript 1.0; Flash Lite 2.0
error:Object - The expression thrown from a throw statement, typically an instance of the Error class or one of its subclasses.
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 after a try block executes. In the following example, setInterval()calls a function every 1000 millisecond (1 second). If an error occurs, an error is thrown and is caught by the catch block. The finally block is always executed whether or not an error occurs. Because setInterval() is used, clearInterval() must be placed in the finally block to ensure that the interval is cleared from memory.
myFunction = function () {
trace("this is myFunction");
};
try {
myInterval = setInterval(this, "myFunction", 1000);
throw new Error("my error");
}
catch (myError:Error) {
trace("error caught: "+myError);
}
finally {
clearInterval(myInterval);
trace("error is cleared");
}
In the following example, the finally block is used to delete an ActionScript object, regardless of whether an error occurred. Create a new AS file called Account.as.
class Account {
var balance:Number = 1000;
function getAccountInfo():Number {
return (Math.round(Math.random() * 10) % 2);
}
}
In the same directory as Account.as, create a new AS or FLA document and enter the following ActionScript in Frame 1 of the Timeline:
import Account;
var account:Account = new Account();
try {
var returnVal = account.getAccountInfo();
if (returnVal != 0) {
throw new Error("Error getting account information.");
}
}
finally {
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 shows the error message in a text field using the Error.toString() method.
In the same directory as Account.as, create a new FLA document and enter the following ActionScript in Frame 1 of the Timeline:
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 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 types of errors, RecordSetException and MalformedRecord.
In the following example, the RecordSetException and MalformedRecord objects are subclasses of the Error class. Each is defined in its own AS class file.
// 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 sortRows() method, one of these previously defined error objects is 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();
}
}
function randomNum():Number {
return Math.round(Math.random() * 10) % 3;
}
}
Finally, in another AS file or FLA script, the following code invokes the sortRows() method on an instance of the RecordSet class. It defines catch blocks for each type of error that is thrown by sortRows()
import RecordSet;
var myRecordSet:RecordSet = new RecordSet();
try {
myRecordSet.sortRows();
trace("everything is fine");
}
catch (e:RecordSetException) {
trace(e.toString());
}
catch (e:MalformedRecord) {
trace(e.toString());
}