java.lang.ArrayIndexOutOfBoundsExcepiton in Java with Examples Last Updated : 02 Feb, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The java.lang.ArrayIndexOutOfBoundsException is a runtime exception and thrown only at the execution state of the program. Java compiler never checks for this error during compilation. The java.lang.ArrayIndexOutOfBoundsException is one of the most common exceptions in java. It occurs when the programmer tries to access the value of an element in an array at an invalid index. This Exception is introduced in Java from JDK Version 1.0 onwards. ArrayIndexOutOfBoundsException can occur due to many reasons like when we try to access the value of an element in the array at a negative index or index greater the size of array -1. Below are the Code Examples showing the cases in which this error can occur and errors are handled and displayed using try-catch block. Case 1:- Accessing The Value Of An Element At An Negative Index Java // Java program to show the ArrayIndexOutOfBoundsException // while accessing element at negative index import java.io.*; class GFG { public static void main(String[] args) { try { int array[] = new int[] { 4, 1, 2, 6, 7 }; // accessing element at index 2 System.out.println("The element at index 2 is " + array[2]); // accessing element at index -1 // this will throw the // ArrayIndexOutOfBoundsException System.out.println("The element at index -1 is " + array[-1]); } catch (Exception e) { System.out.println(e); } } } OutputThe element at index 2 is 2 java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 Case 2:- Accessing The Element At Index Greater Then Size of Array -1 Java // Java program to show the ArrayIndexOutOfBoundsException // while accessing element at index greater then size of // array -1 import java.io.*; class GFG { public static void main(String[] args) { try { int array[] = new int[] { 4, 1, 2, 6, 7 }; // accessing element at index 4 System.out.println("The element at index 4 is " + array[4]); // accessing element at index 6 // this will throw the // ArrayIndexOutOfBoundsException System.out.println("The element at index 6 is " + array[6]); } catch (Exception e) { System.out.println(e); } } } OutputThe element at index 4 is 7 java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 5 Comment More infoAdvertise with us Next Article Arrays.copyOfRange() in Java with Examples L lavishgarg26 Follow Improve Article Tags : Java Technical Scripter Technical Scripter 2020 Java-Exceptions Practice Tags : Java Similar Reads Arrays.copyOfRange() in Java with Examples Arrays copyOfRange() method is used to create a new array by copying a specified range of elements from an existing array. It provides a way to copy a subset of elements between two indices by creating a new array of the same type.Below is a simple example that uses Arrays.copyOfRange() method to co 4 min read Arraylist removeRange() Method in Java with Examples The removeRange() method of the ArrayList class in Java is used to remove all elements from the list within the specified range of indices. This method removes the elements from the starting index (fromIndex) to the ending index (toIndex). Example 1: Here, we use the removeRange() method to remove a 3 min read Arraylist lastIndexOf() Method in Java with Examples In Java, the lastIndexOf() method is used to find the index of the last occurrence of a specified element in an ArrayList.Example 1: Here, we will use the lastIndexOf() method to find the index of the last occurrence of a specific element in an ArrayList of integers.Java// Java program to demonstrat 3 min read Arrays.binarySearch() in Java with Examples | Set 1 In Java, the Arrays.binarySearch() method searches the specified array of the given data type for the specified value using the binary search algorithm. The array must be sorted by the Arrays.sort() method before making this call. If it is not sorted, the results are undefined. Example:Below is a si 3 min read Array Index Out Of Bounds Exception in Java In Java, ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program. It occurs when we try to access the element out of the index we are allowed to, i.e. index >= size of the array.Java supports 4 min read Difference Between StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException in Java An unexpected, unwanted event that disturbed the normal flow of a program is called an Exception. Most of the time exceptions are caused by our program and these are recoverable. Example: If our program requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file 3 min read Like