Class

# IllegalCastException

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

## Description

Occurs when you attempt to cast a `Variant</api/data_types/variant>` or object to an incompatible object type.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                            | Type                               | Read-Only | Shared |
|-------------------------------------------------|------------------------------------|-----------|--------|
| `ErrorNumber<illegalcastexception.errornumber>` | `Integer</api/data_types/integer>` |           |        |
| `Message<illegalcastexception.message>`         | `String</api/data_types/string>`   |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                             | Parameters                                                                                              | Returns                                    | Shared |
|--------------------------------------------------|---------------------------------------------------------------------------------------------------------|--------------------------------------------|--------|
| `Constructor<illegalcastexception.constructor0>` | message As `String</api/data_types/string>` = "", errorNumber As `Integer</api/data_types/integer>` = 0 |                                            |        |
| `Stack<illegalcastexception.stack>`              |                                                                                                         | `String()</api/data_types/string>`         |        |
| `StackFrames<illegalcastexception.stackframes>`  |                                                                                                         | `StackFrame()</api/exceptions/stackframe>` |        |

## Property descriptions

<div id="illegalcastexception.errornumber">

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

</div>

<div class="rst-class">

forsearch

</div>

IllegalCastException.ErrorNumber

**ErrorNumber** As `Integer</api/data_types/integer>`

> Used to contain an error number that describes the runtime error.

<div id="illegalcastexception.message">

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

</div>

<div class="rst-class">

forsearch

</div>

IllegalCastException.Message

**Message** As `String</api/data_types/string>`

> Used to contain descriptive text to display when the runtime exception is encountered.

## Method descriptions

<div id="illegalcastexception.constructor0">

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

</div>

<div class="rst-class">

forsearch

</div>

IllegalCastException.Constructor

**Constructor**(message As `String</api/data_types/string>` = "", errorNumber As `Integer</api/data_types/integer>` = 0)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Used to raise your own `RuntimeException</api/exceptions/runtimeexception>` with a *message* and optional error number.

<div id="illegalcastexception.stack">

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

</div>

<div class="rst-class">

forsearch

</div>

IllegalCastException.Stack

**Stack** As `String()</api/data_types/string>`

> Returns a `String</api/data_types/string>` array that contains a list of all of the methods in the stack from the main entrypoint to the point at which the exception was invoked.
>
> The stack contains all the method names up and including the current method name.
>
> This feature only works if the **IncludeFunctionNames** property on the App object is selected in the Shared Build Settings.
>
> In addition to your own method calls, you will also see framework method calls, but these may not always be completely accurate due to insufficient symbols for the OS to resolve.

<div id="illegalcastexception.stackframes">

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

</div>

<div class="rst-class">

forsearch

</div>

IllegalCastException.StackFrames

**StackFrames** As `StackFrame()</api/exceptions/stackframe>`

> Returns an array containing the stack when the exception was first raised.

## Notes

An <span class="title-ref">IllegalCastException</span> occurs when you attempt to cast a `Variant</api/data_types/variant>` or object to an incompatible object type. The exception is thrown at the point of the cast itself, regardless of whether any methods or properties are subsequently accessed.

For example, casting a `DesktopBevelButton</api/user_interface/desktop/desktopbevelbutton>` as a `DesktopButton</api/user_interface/desktop/desktopbutton>` throws an <span class="title-ref">IllegalCastException</span> because a `DesktopBevelButton</api/user_interface/desktop/desktopbevelbutton>` is not a `DesktopButton</api/user_interface/desktop/desktopbutton>`.

An <span class="title-ref">IllegalCastException</span> also occurs when assigning a `Variant</api/data_types/variant>` to an incompatible object type:

``` xojo
Try // Variant containing a scalar assigned to an object type
  Var v As Variant = 123
  Var d As Dictionary = v
Catch Err As IllegalCastException
End Try

Try // Variant containing an object assigned to an incompatible object type
  Var v As Variant = New Picture(1, 1)
  Var d As Dictionary = v
Catch Err As IllegalCastException
End Try
```

See `TypeMismatchException</api/exceptions/typemismatchexception>` for the related exception that occurs when assigning a `Variant</api/data_types/variant>` to an incompatible scalar type.

## Sample code

This example attempts to cast a `DesktopBevelButton</api/user_interface/desktop/desktopbevelbutton>` as a `DesktopButton</api/user_interface/desktop/desktopbutton>` then call the Press method of the `DesktopButton</api/user_interface/desktop/desktopbutton>` class. This example assumes that there is a `DesktopBevelButton</api/user_interface/desktop/desktopbevelbutton>` called "BevelButton1" in the same window where this code is being called:

``` xojo
Var c As DesktopUIControl
c = BevelButton1
DesktopButton(c).Press
```

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

When you test this code in the IDE, execution will stop at the last line and display an <span class="title-ref">IllegalCastException</span> error in your Code editor. If you test the code in a standalone app, the application will display a generic error message and quit when the user clicks OK.

You can handle the error by adding an `Exception</api/exceptions/exception>` block to your code:

``` xojo
Var c As DesktopUIControl
c = BevelButton1
DesktopButton(c).Press
Exception err
  If err IsA IllegalCastException Then
    MessageBox("Trying to recast a DesktopBevelButton as DesktopButton!")
  End If
```

## Compatibility

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

<div class="seealso">

`RuntimeException</api/exceptions/runtimeexception>` parent class; `TypeMismatchException</api/exceptions/typemismatchexception>`; `Function</api/language/function>`, `Raise</api/language/raise>` statements; `Nil</api/language/nil>` datatype; `Exception</api/exceptions/exception>`, `Try</api/language/try>` statements.

</div>
