Keyword

# Catch

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to handle `RuntimeException</api/exceptions/runtimeexception>` errors in a `Try</api/language/try>` statement.

## Usage

``` xojo
Catch [ [ ErrorParameter As ] ErrorType ]
```

| Part           | Description                                                                                                                                               |
|----------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| ErrorParameter | Optional: Used to determine the type of runtime exception.                                                                                                |
| ErrorType      | Optional: Used to <span class="title-ref">catch</span> a particular type of runtime error. This can only be used in conjunction with an *ErrorParameter*. |

## Notes

The `Try</api/language/try>` statement is similar to the `Exception</api/exceptions/exception>` statement. Exceptions that occur within the `Try</api/language/try>` block are caught by the <span class="title-ref">Catch</span> statement. It has the same syntax as the `Exception</api/exceptions/exception>` statement. See the `RuntimeException</api/exceptions/runtimeexception>` statement or the Runtime Errors theme for descriptions of each type of runtime error.

Unlike the `Exception</api/exceptions/exception>` statement, `Try</api/language/try>` statements can be nested. If a `Try</api/language/try>` statement does not handle an exception or re-raises it, it can be handled by the next outermost `Try</api/language/try>` statement, or by the `Exception</api/exceptions/exception>` statement itself if there are no containing `Try</api/language/try>` statements.

Local variables that are declared inside a <span class="title-ref">Catch</span> statement exist only within the <span class="title-ref">Catch</span> statement's scope rather than inside the whole method's scope. This means that multiple <span class="title-ref">Catch</span> statements at the same level can use the same exception variable name.

A `Try</api/language/try>` block can contain its own `Finally</api/language/finally>` statement. The code in the `Finally</api/language/finally>` statement will execute regardless of whether or not an exception was raised within the `Try</api/language/try>` block.

## Sample code

This example uses the <span class="title-ref">Catch</span> statement to handle an out of range key that is passed to a `Dictionary</api/language/dictionary>`.

``` xojo
Try
  Var a(5) As Integer
  a(6) = 45 ' Raises an OutOfBoundsException
Catch e As OutOfBoundsException
  ' The above exception is caught here for you to handle
  ErrorLabel.Text = "Exceeded size of array"
End Try
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can also <span class="title-ref">Catch</span> multiple exceptions:

``` xojo
Try
  Var f As New FolderItem("test.txt", FolderItem.PathModes.Native)

  ListBox1.CellTextAt(10, 10) = "Hello"
Catch noe As NilObjectException
  ' code
Catch oobe As OutOfBoundsException
  ' code
End Try
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Exception</api/exceptions/exception>`, `Try</api/language/try>` statements; `Finally</api/language/finally>`, `Function</api/language/function>`, `Sub</api/language/sub>` statements; `RuntimeException</api/exceptions/runtimeexception>` error.

</div>
