Handling array index out of bounds exception

Report a typo

In a Java program, you've arrays of integers and you want to fetch an element from an index. Given a scenario where you might attempt to access an index that is not within the array size (which would traditionally produce an error), fill the blanks in the provided code to handle this situation gracefully, and avoid the program from terminating abruptly. The message 'Array Index is Out Of Bounds' should be printed when such an event occurs.

Fill in the gaps with the relevant elements
public class Main {
    public static void main(String[] args) {
         {
            int[] arr = new int[5];
            System.out.println(arr[7]);
        }  ( e) {
            System.out.println("Array Index is Out Of Bounds");
        }
    }
}
catchArrayIndexOutOfBoundsExceptiontry

Create a free account to access the full topic