Keyword

# Exception

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

## Description

Used to handle `RuntimeException</api/exceptions/runtimeexception>` errors.

## Usage

``` xojo
Exception [ [ ErrorParameter As ] ErrorType ]
  ' User code
Finally 
  ' Code that executes even if runtime exceptions were raised
```

| Part           | Description                                                                                                                  |
|----------------|------------------------------------------------------------------------------------------------------------------------------|
| ErrorParameter | Optional: Used to determine the type of runtime exception.                                                                   |
| ErrorType      | Optional: Used to 'catch' a particular type of runtime error. This can only be used in conjunction with an *ErrorParameter*. |

## Notes

The <span class="title-ref">Exception</span> statement catches runtime exceptions that occur anywhere within the method or event. In most cases using a `Try</api/language/try>` is better since it allows you to catch exceptions in specific parts of your code.

One or more <span class="title-ref">Exception</span> statements can be inserted after the last "regular" line of code to catch and handle runtime exceptions that may occur anywhere within the method. Local variables that are declared inside an <span class="title-ref">Exception</span> block exist only within the block's scope rather than inside the entire method's scope. This means that multiple <span class="title-ref">Exception</span> statements at the same level can use the same <span class="title-ref">Exception</span> variable name.

If a runtime <span class="title-ref">Exception</span> occurs in a built application and is not handled, a generic runtime error message box is displayed and the app is quits. <span class="title-ref">Exception</span> statements provide a means of handling the error more gracefully.

Exception statements always appear at the end of a method (not where you think the error might occur) because every line after the <span class="title-ref">Exception</span> line is considered part of the <span class="title-ref">Exception</span> block. In the Code Editor, the <span class="title-ref">Exception</span> line has the same level of indentation as the `Sub</api/language/sub>` or `Function</api/language/function>` line.

You can use <span class="title-ref">Exception</span> alone if you wish to handle any type of <span class="title-ref">Exception</span> in the <span class="title-ref">Exception</span> block, as shown below:

``` xojo
Sub MyMethod()

Exception
  MessageBox("Something really bad happened, but I don't know what.")
End Sub
```

The example shown above is sufficient to prevent the application from quitting, but the message is not very informative because you don't have a clue what type of <span class="title-ref">Exception</span> occurred.

The way to determine which type of runtime error occurred is to use *ErrorParameter* and, in some way, test its type. *ErrorParameter* can be any of the `RuntimeException</api/exceptions/runtimeexception>` subclasses.

One way to test *ErrorParameter* is with an `If</api/language/if>` statement in the <span class="title-ref">Exception</span> block:

``` xojo
Sub MyMethod()

Exception err
  If err IsA TypeMismatchException Then
    MessageBox("Tried to retype an object!")
  ElseIf err IsA NilObjectException Then
    MessageBox("Tried to access a Nil object!")
  End If
End Sub
```

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

### The preferred way

Instead of using multiple `If</api/language/if>` statements, you can use multiple <span class="title-ref">Exception</span> statements, each of which handles a different runtime <span class="title-ref">Exception</span> type. This is the preferred way to manage exceptions. Note that you can use the same variable name several times, like *err* in the following example. An <span class="title-ref">Exception</span> will be caught only once. As a consequence, a `NilObjectException</api/exceptions/nilobjectexception>` would be caught only by the *\`Exception\` err as NilObjectException* block and not by the last *\`Exception\` err as RuntimeException* (which is not recommended, anyway. See `EndException</api/exceptions/endexception>` and `ThreadEndException</api/exceptions/threadendexception>`).

``` xojo
Sub MyMethod()

Exception err As TypeMismatchException
  MessageBox("Tried to retype an object!")
Exception err As NilObjectException
  MessageBox("Tried to access a Nil object!")
Exception err As RuntimeException ' NOT RECOMMENDED
  MessageBox("Another exception")
End Sub
```

You should not use `Return</api/language/return>` in an <span class="title-ref">Exception</span> block as it would prevent the `Finally</api/language/finally>` block to execute, if any.

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

### EndException and ThreadEndException

Under some circumstances, an <span class="title-ref">Exception</span> block can catch an `EndException</api/exceptions/endexception>` or a `ThreadEndException</api/exceptions/threadendexception>`. This happens when you use a *catch-all* <span class="title-ref">Exception</span> statement, i.e. "<span class="title-ref">Exception</span> err as RuntimeException" instead of giving a more specific runtime <span class="title-ref">Exception</span> class. In such a case, you MUST re-raise the <span class="title-ref">Exception</span> to avoid messing up the runtime environment and create some unpredictable problems.

``` xojo
Exception err As RuntimeException ' Will catch any exception without discrimination

  If err IsA EndException Or err IsA ThreadEndException Then
    Raise err ' Re-raise the exception
  End If

  ' Continue your code here
```

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

### Alternative

The `Try</api/language/try>` block is an alternative to the <span class="title-ref">Exception</span> block. Unlike <span class="title-ref">Exception</span>, you can have nested `Try</api/language/try>` statements. If the innermost `Try</api/language/try>` block does not handle the <span class="title-ref">Exception</span>, it will be passed to the next block, and so forth. If both `Try</api/language/try>` and <span class="title-ref">Exception</span> statements are used together, the `Try</api/language/try>` block will catch errors prior to the <span class="title-ref">Exception</span> block.

## Compatibility

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

<div class="seealso">

`Function</api/language/function>`, `Raise</api/language/raise>`, `Sub</api/language/sub>`, `Try</api/language/try>` statements.

</div>
