/**
* @author lautturi.com
* Java example: java index out of bound exception example
*/
import java.util.*;
import java.time.*;
import java.time.format.*;
public class Lautturi {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
System.out.println(arr[11]);
}
}
output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
ArrayIndexOutOfBoundsException occurs when the index used to address array items exceeds the allowed value.
The last index is 10.
try {
System.out.println(arr[index]);
}
catch(ArrayIndexOutOfBoundsException exception) {
handleTheExceptionSomehow(exception);
}