This exception occurs when trying to access an index which is out of array bounds (bigger than array.length-1 )
/**
* @author lautturi.com
* Java example: arrayindexoutofboundsexception array index out of bound exception example
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
try {
// divided by 0 will trigger an exception
int[] array = {1,2,3,4};
System.out.println(array[5]);
}
catch (Exception e) {
System.out.println(e);
System.out.println("Exception: " + e.getMessage());
}
}
}
output:
java.lang.ArrayIndexOutOfBoundsException: 5 Exception: 5