Java error ArrayIndexOutofBoundsException

When an array has been accessed with an illegal index, which doesn't exist then ArrayIndexOutofBoundsException is thrown.

Java error ArrayIndexOutofBoundsException

Java error: ArrayIndexOutofBoundsException

In this section you will get details about ArrayIndexOutofBoundsException java. This exception generally occurs when accessing to an illegal array index. The index is either greater or equal to the size of array. Suppose you have created an array with size of five and trying to access the sixth array element which does not exist, then it will throw an ArrayIndexOutofBoundsException. It means that you have created a array which store five value and you are trying to access the sixth element which doesn't exist.

Example: Code to show the ArrayIndexOutofBoundsException error


public class ArrayIndex {
	public static void main(String args[])
	{
		int b[]={10,20,30,40,50};
		System.out.println(b[6]);
		}
    }

In the above code, creating an array with size five which contain five integer element and printing the sixth element which does not exist. So, it will throw a exception ArrayIndexOutofBoundsException.

Output : When you will compile and execute then output will look like as below: