Error: ArrayIndexOutOfBounds Exception

ArrayIndexOutOfBounds Exception is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
It is a Runtime exception.

Consider the following example below:

class abc { 
public static void main(String args[]) 
    { 
        try { 
            int arr[] = new int[5]; 
            arr[8] = 2; // accessing 9th element in an array of size 5
        } 
        catch (ArrayIndexOutOfBoundsException e) { 
            System.out.println("ArrayIndexOutOfBounds Exception Encountered"); 
        } 
    } 
}

The above example will display the following message:
ArrayIndexOutOfBounds Exception Encountered

This can be rectified by:

Making the array Index access the array within its limited size.

The Required solution is:

 class abc { 
    public static void main(String args[]) 
        { 
            try { 
                int arr[] = new int[5]; 
                arr[4] = 2; // accessing 5th element in an array of size 5
            } 
            catch (ArrayIndexOutOfBoundsException e) { 
                System.out.println("ArrayIndexOutOfBounds Exception Encountered"); 
            } 
        } 
    }

Now the Value of 2 is assigned to the 5th element of the array and hence there is no exception being raised.

Now, you can add a breakpoint in your respective Java file, and use the VS CODE debugger to find the exact point where this occurs and resolve this exception.

You can refer to the following link to understand more about this exception in Java.