ArrayIndexOutOfBoundsException is a runtime exception that occurs when a program tries to access an array element using an invalid index. In Java, valid array indices range from 0 to array.length - 1. If an index is negative or greater than or equal to the array length, Java throws an ArrayIndexOutOfBoundsException to prevent illegal memory access.
- Java automatically checks array bounds before accessing elements.
- It can be prevented using proper index validation.
- Enhanced for-loops help avoid index-related errors.
public class GFG {
public static void main(String[] args)
{
// taking array of integers
int a[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i <= a.length; i++)
System.out.println(a[i]);
}
}
Runtime Error Throws an Exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at GFG.main(GFG.java:11)
Here if you carefully see, the array is of size 5. Therefore while accessing its element using for loop, the maximum index value can be 4, but in our program, it is going till 5 and thus the exception.
Example: Program to show ArrayIndexOutOfBoundsException when we access the negative index of array.
public class GFG {
public static void main(String[] args)
{
// taking array of integers
int a[] = { 1, 2, 3 };
// accessing the negative index of array
System.out.println(a[-2]);
}
}
Run-Time Exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -2 out of bounds for length 3
at GFG.main(GFG.java:12)
Handling ArrayIndexOutOfBoundsException in Java
To handle ArrayIndexOutOfBoundsException, make sure that index of array is within the valid range. You can also use the enhanced for-loop to automatically handle this exception.
Example: Program to handle ArrayIndexOutOfBoundsException by taking array index within valid range
public class GFG {
public static void main(String[] args)
{
// taking array of integers
int a[] = { 1, 2, 3, 4, 5 };
// here, we have remove equal to sign
for (int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
}
Output
1 2 3 4 5
Explanation: The loop runs from index 0 to a.length - 1 using the condition i < a.length. Since all accessed indices are valid, the program prints all elements successfully without throwing any exception.
Example: Program to handle ArrayIndexOutOfBoundsException by using enhanced for loop
public class GFG {
public static void main(String[] args)
{
// taking array of integers
int a[] = { 1, 2, 3, 4, 5 };
// using enhanced for loop
for (int e : a) {
System.out.println(e);
}
}
}
Output
1 2 3 4 5
Explanation: The enhanced for-loop automatically traverses all array elements without requiring index management. Since there is no direct index access, the possibility of an ArrayIndexOutOfBoundsException due to incorrect indexing is eliminated.
Example: Program to handle ArrayIndexOutOfBoundsException by using using try-catch block
public class GFG {
public static void main(String[] args)
{
// taking array of integers
int a[] = { 1, 2, 3, 4, 5 };
// using try catch block
try {
for (int i = 0; i <= a.length; i++)
System.out.print(a[i] + " ");
}
catch (Exception e) {
System.out.println("\nException Caught");
}
}
}
Explanation :The loop still contains the incorrect condition i <= a.length, which causes an exception when index 5 is accessed. However, the exception is caught by the catch block, preventing the program from terminating abruptly and displaying "Exception Caught" instead.