ArrayIndexOutOfBoundsException
The ArrayIndexOutOfBoundsException occurs when you are trying to access an item in an array that does not exist.
The following code will throw the ArrayIndexOutOfBoundsException:
String[] anArray = new String[5];
anArray[0] = "first element";
anArray[1] = "second element";
anArray[2] = "third element";
anArray[3] = "fourth element";
anArray[4] = "fifth element";
// the following code will throw and catch the exception
try
{
System.out.println(anArray[5]);
} catch(ArrayIndexOutOfBoundsException e)
{
System.err.println("ArrayIndexOutOfBoundsException occured");
e.printStackTrace();
}
The output for this code will be:
ArrayIndexOutOfBoundsException occured
java.lang.ArrayIndexOutOfBoundsException: 5
at yourpackage.TheClassName.methodname(TheClassName.java:19)
