# try..catch..finally

#### Availability

Flash Player 7.

#### Usage

    try {
    	// ... try block ...
    } finally {
    	// ... finally block ...
    }
    try {
    	// ... try block ...
    } catch(variable) {
    	// ... catch block ...
    }
    try {
    	// ... try block ...
    } catch (error)
    	// ... catch block ...
    } finally {
    	// ... finally block ...
    }

#### Parameters

`variable` An identifier for the error object thrown from the `try` clause.

#### Description

Keywords; enclose a block of code in which an error can occur. If any code within the `try` code block throws an exception (using the `throw` action), control passes to the `catch` block, if one exists, then to the `finally` code block, if one exists. If the `try` block doesn't throw an exception (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. Each `try` block can have only one `catch` block. However, you can throw instances of custom error objects and then use the `instanceof` operator to determine the type of error thrown.

Each `try` block can have only one `finally` block.

The Flash Player itself does not throw runtime errors; you must throw errors explicitly by using the `throw` action. (See `throw`.)

You can nest `try` blocks as many levels deep as desired.

A new object is created and the value that was thrown is placed in the variable slot of the object. This object is appended to the scope chain as the "innermost" object. The catch block runs with this object on the scope chain; when the catch block exits, the object is removed from the scope chain and disposed.

The variable in catch must be a simple identifier such as e or theException or x.

The finally block is optional and always executes, regardless of whether an exception was thrown or not.

#### Example

Usage 1: This 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 `try` block attempts to create an XMLSocket connection using the `XMLSocket.connect` method, which returns `true` if the connection was successful. The `finally` block is used to close the socket connection, if one exists.

    var sock = new XMLSocket();
    try {
    	// Try to connect to server, throwing exception
    	// if it fails.
      if (!sock.connect("www.mybiz.com", 8080)) {
        throw new Error("Couldn't connect socket.");
      }
    	// ... code to use the XMLSocket object here ...
    } finally {
      // Close socket, whether or not an exception occurred.
      if (sock != null) {
        sock.close();
      }
    }

Usage 2: This 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.

A new object is created and the value that was thrown is placed in the variable slot of the object. This object is appended to the scope chain as the "innermost" object. The catch block runs with this object on the scope chain; when the catch block exits, the object is removed from the scope chain and disposed.

The variable referenced in the `catch` block must be a simple identifier such as `e`, or `theException`.

Usage 3: This example demonstrates a `try..catch..finally` statement.

#### See also

Error object, `throw`