In this tutorial, we will how to handle ArrayIndexOutOfBoundsException. This exception is a run time error that occurs whenever a formula references specify an array with an index that is outside array defined boundaries. In the example below,we have passed two command line argument and in our program we try to access the third argument which is not present then it throws an ArrayIndexOutOfBoundsException. System.out.println("args[2]="+args[2]); throws ArrayIndexOutOfBoundsException. Since no value is present on the array index 2.

Java Code: Program that throws ArrayIndexOutOfBoundsException
public class ArrayoutofboundException {

    public static void main(String[] args) {
        try {
            System.out.println("args[0]=" + args[0]);
            System.out.println("args[1]=" + args[1]);
            System.out.println("args[2]=" + args[2]);
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}
Output of the program

java.lang.ArrayIndexOutOfBoundsException: 0