Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range. Applications can subclass this class to indicate similar exceptions.
The IndexOutOfBoundsException
is thrown when attempting to access an invalid index within a collection, such as an array.
public class IndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
int[] arr = new int[5];
try {
System.out.println(arr[6]);
} catch (IndexOutOfBoundsException e) {
System.out.println("This is your problem:");
System.out.println("Error: " + e);
}
}
}
This is your problem:
Error: java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 5