OutOfBoundsException

From Xojo Documentation

Class (inherits from RuntimeException)

Occurs when code tries to access an array element that doesn't exist.

Properties
ErrorNumber Message Reason fa-lock-32.png
Methods
Stack StackFrames

Notes

An OutOfBoundsException error occurs when your code uses an array index that is out of range. This occurs, for example, if you forget that arrays are zero-based and you make an error when you try to access the last element.

Sample Code

This example is supposed to display the fonts installed on the user's computer in a ListBox. It generates an OutOfBoundsException because the loop should be from 0 to nFonts-1 rather than from 1 to nFonts. When the loop gets to nFonts, the value of i is out of range and the error occurs.

Var nFonts As Integer
nFonts = FontCount
For i As Integer = 1 To nFonts
ListBox1.Addrow(Font(i))
Next

If you run this code in a built application, you will see a generic error message when i reaches the value of nFonts:

An exception of class OutOfBoundsException was not handled. The application must shut down.

You can handle the exception by adding the following code:

Var nFonts As Integer
nFonts = FontCount
For i = 1 To nFonts
ListBox1.AddRow(Font(i))
Next

Exception err
If err IsA OutOfBoundsException Then
MessageBox("The value: " + i.ToString + " is out of range!")
End If

When this code runs in a built application, the user sees a dialog box that gives the value of the out of range number. The application does not quit when the user accepts the dialog box.

See Also

RuntimeException class; Function, Raise, Sub statements; Nil datatype, Exception, Try statements.