Keyword

# Finally

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

## Description

Contains code that will run after a method or function is finished, even if a `RuntimeException</api/exceptions/runtimeexception>` has occurred.

## Usage

``` xojo
Try
  ' Your code
Catch
  ' code that executes if run time exceptions were raised
Finally
  ' code that executes even if runtime exceptions were raised
End Try
```

## Notes

Sometimes a method needs to do some cleanup work whether it is finishing normally or aborting early because of an `exception</api/exceptions/runtimeexception>`. The optional <span class="title-ref">Finally</span> block at the end of a method or function runs after its exception handlers, if it has any. Code in this block will be executed even if an exception has occured, whether the exception was handled or not.

More commonly you will use <span class="title-ref">Finally</span> as part of a `Try</api/language/try>` statement.

For example, you can write:

``` xojo
Sub SomeFunction()
  ' some code here...
Finally
  ' some more code that will always execute (unless a Return statement is used, see below)
End Sub
```

However, the code in the <span class="title-ref">Finally</span> block does *not* execute if you exit the method with a `Return</api/language/return>` statement. See the following code:

``` xojo
Sub SomeFunction(x As Integer)
  If x = 0 Then
    Return ' -> the Finally block won't execute
  End If
Finally
  Break ' Debugger would stop here to check if it executes
End Sub
```

See also the entries for the `Sub</api/language/sub>` and `Function</api/language/function>` statements.

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

### Returning from a <span class="title-ref">Finally</span> block

You should not use `Return</api/language/return>` statements within a <span class="title-ref">Finally</span> block. The execution of a `Return</api/language/return>` statement will cause any exceptions raised inside the `Try</api/language/try>` and Catch blocks to be lost to the caller.

Here is an example:

``` xojo
Sub Test()
  Raise New OutOfBoundsException
Finally
  Return ' This is a bad idea!
End Sub
```

If you call this Test method, it will return as if there had been no exception raised inside. Therefore, avoid Return statements at all costs. If you really want to drop any raised exceptions, it is suggested that you be more explicit of that intention by adding a Catch block.

Also note that because a `Return</api/language/return>` statement will skip any following <span class="title-ref">Finally</span> blocks, code that requires cleanup should be nested in its own `Try</api/language/try>` block, with any return statement outside that block. <span class="title-ref">Finally</span> blocks at the method level are better avoided.

## Sample code

This simple example shows a <span class="title-ref">Finally</span> block that executes even after an exception.

``` xojo
Try
  Raise New RuntimeException
Finally
  ' Executes even though the exception is not handled
  MessageBox("Finally")
End Try
```

## Compatibility

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

<div class="seealso">

`Catch</api/language/catch>`, `Function</api/language/function>`, `Sub</api/language/sub>` statements; `Exception</api/exceptions/exception>`, `Try</api/language/try>` statements.

</div>
