Keyword

# Try

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

## Description

Used to handle `RuntimeException</api/exceptions/runtimeexception>` errors prior to the `Exception</api/exceptions/exception>` statement.

## Usage

``` xojo
Try
  ' Your code
Catch [ [ errorParameter As ] ErrorType ]
  ' Your code to handle the exception
Finally
  ' Your code that will execute even if an exception has occurred
End [ Try ]
```

| Part                                                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|-------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| <span class="title-ref">Try</span>                          | The code that that while executing will results in the Catch section executing should an exception occur.                                                                                                                                                                                                                                                                                                                                                                                                     |
| `Catch</api/language/catch>`                                | Optional. This code executes should an exception occur. The optional *errorParameter* allows you to be specific about the type of exception (`IOException</api/exceptions/ioexception>`, `RuntimeException</api/exceptions/runtimeexception>`, etc.) you wish to catch. The *errorType* is also optional but can only be used in conjunction with an *errorParameter*. A <span class="title-ref">Try</span> Catch statement can continue multiple Catch statements that handle different types of exceptions. |
| `Finally</api/language/finally>`                            | Optional. This code that executes regardless of whether or not an exception occurred.                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `End</api/language/end>` <span class="title-ref">Try</span> | Signifies the end of the <span class="title-ref">Try</span> Catch statement. The <span class="title-ref">Try</span> keyword here is optional.                                                                                                                                                                                                                                                                                                                                                                 |

## Notes

The <span class="title-ref">Try</span> statement is similar to the `Exception</api/exceptions/exception>` statement. Exceptions that occur within the <span class="title-ref">Try</span> statement are caught by the `Catch</api/language/catch>` 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, <span class="title-ref">Try</span> statements can be nested. If a <span class="title-ref">Try</span> statement does not handle an exception or re-raises it, it can be handled by the next outermost <span class="title-ref">Try</span> statement, or by the `Exception</api/exceptions/exception>` statement itself if there are no containing <span class="title-ref">Try</span> statements.

When an exception occurs inside a <span class="title-ref">Try</span> block, execution of the remaining code in that block stops immediately, regardless of whether a `Catch</api/language/catch>` statement is present. Without a <span class="title-ref">Catch</span>, the exception is not handled and propagates up the call stack. This means that a <span class="title-ref">Try</span> block without a <span class="title-ref">Catch</span> does not allow code to continue after an exception — it only ensures that a `Finally</api/language/finally>` block (if present) will execute before the exception propagates.

A <span class="title-ref">Try</span> statement 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 <span class="title-ref">Try</span> statement. However, the code in the <span class="title-ref">Finally</span> block does *not* execute if you exit the <span class="title-ref">Try</span> or <span class="title-ref">Catch</span> statement with a `Return</api/language/return>` statement. Please note that Android is an exception here. Kotlin also calls <span class="title-ref">Finally</span> statements after the <span class="title-ref">Return</span> statement has been used.

## Sample code

This example uses the `Catch</api/language/catch>` statement in a window's Opening event handler to handle out of memory exceptions when trying to draw an imported gif image. The variable MyPicture is a global property of type `Picture</api/graphics/picture>`. The picture "Logo" has been added to the Project.

``` xojo
Try
  MyPicture = New Picture(Logo.Width, Logo.Height)
  MyPicture.Graphics.DrawPicture(Logo, 0, 0)
Catch err As OutOfMemoryException
  MessageBox("Insufficient memory to draw the picture!")
End Try
```

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

The following example handles an attempt to access a nonexistent value in a `Dictionary</api/language/dictionary>`.

``` xojo
Try
  someValue = myDict.Value("doesn't exist")
Catch err As KeyNotFoundException
  MessageBox("The requested key does not exist.")
End Try
```

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

The following catches any exception in the code block:

``` xojo
Try
  Var d As DateTime
  Var day As Integer = d.Day ' NilObjectException will be caught below
Catch e As RunTimeException
  If e IsA EndException Or e IsA ThreadEndException Then
    Raise e ' Re-raise the exception for the framework
  End If

  MessageBox(e.Message)
End Try
```

## Compatibility

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

<div class="seealso">

`Catch</api/language/catch>`, `Exception</api/exceptions/exception>`, `Finally</api/language/finally>`, `Function</api/language/function>`, `Raise</api/language/raise>`, `Sub</api/language/sub>` statements; `RuntimeException</api/exceptions/runtimeexception>` class; `Exception Handling</getting_started/debugging/exception_handling>` topic

</div>
