try..catch..finally

Availability

Flash Player 7.

Usage

try {
  // ... try block ...
} finally {
  // ... finally block ...
}
try {
  // ... try block ...
} catch(error[:ErrorType1]) {
  // ... catch block ...
} [catch(error[:ErrorTypeN]) {
  // ... catch block ...
}] [finally {
  // ... finally block ...
}]

Parameters

error The expression thrown from a throw statement, typically an instance of the Error class or a subclass thereof.

ErrorType An optional type specifier for the error identifier. The catch clause only catches errors of the specified type.

Description

Keywords; 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 action), control passes to the catch block, if one exists, 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.

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 Creating Classes with ActionScript 2.0.)

// 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 are thrown depending on the type of exception that occurred. The following code snippet shows how this code might look.

// Within RecordSet.as class file...
function sortRows() {
  ...
  if(recordSetErrorCondition) {
    throw new RecordSetException();
  }
  if(malFormedRecordCondition) {
    throw new MalformedRecord();
  }
  ...
}  

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().

try {
  myRecordSet.sortRows();
} catch (e:RecordSetException) {
  trace("Caught a recordset exception");
} catch (e:MalformedRecord) {
  trace("Caught a malformed record exception");
}

See also

Error class, throw, class, extends