java.lang.ArrayIndexOutOfBoundsException is a runtime exception, so it’s a unchecked exception and don’t need to be thrown explicitly from method. Refer Exception Handling in java
java.lang.ArrayIndexOutOfBoundsException
- ArrayIndexOutOfBoundsException is thrown to indicate that we are trying to access array element with an illegal index.
- This exception is thrown when the index is either negative or greater than or equal to the size of the array.
ArrayIndexOutOfBoundsException Class Diagram
ArrayIndexOutOfBoundsException super classes are Exception
, RuntimeException
and IndexOutOfBoundsException
.
java.lang.ArrayIndexOutOfBoundsException Example
Let’s look at a simple example where our program may throw ArrayIndexOutOfBoundsException based on user input.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.journaldev.exceptions; import java.util.Scanner; public class ArrayIndexOutOfBoundsExceptionExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter size of int array:"); int size = sc.nextInt(); int[] intArray = new int[size]; for (int i = 0; i < size; i++) { System.out.println("Please enter int value at index " + i + ":"); intArray[i] = sc.nextInt(); } System.out.println("Enter array index to get the value:"); int index = sc.nextInt(); sc.close(); System.out.println("Value at " + index + " = " + intArray[index]); } } |
Below log shows one of the execution of above program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
Enter size of int array: 3 Please enter int value at index 0: 1 Please enter int value at index 1: 2 Please enter int value at index 2: 3 Enter array index to get the value: 4 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at com.journaldev.exceptions.ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:23) |
So our program can throw ArrayIndexOutOfBoundsException because of illegal input from user. Also notice that exception stack trace prints the illegal index causing the exception.
How to fix ArrayIndexOutOfBoundsException
We shouldn’t try to recover from this exception, we should try to mitigate it by checking if the index value passed is a valid value or not. Also note that this situation can occur mostly in case of user input, if we are creating array ourself and iterating over it’s elements then chances of this exception are less.
Below code snippet shows the small change in program while taking user input to access the element, if value passed is invalid then a warning message is shown to pass the valid value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
boolean exit = false; while (!exit) { System.out.println("Enter array index to get the value:"); int index = sc.nextInt(); if (index < 0 || index >= size) { System.out.println("Valid index range is from 0 to " + (size - 1)); } else { System.out.println("Value at " + index + " = " + intArray[index]); exit = true; //to terminate the program sc.close(); //close resources } } |
Now the output will be like below when illegal index is passed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
Enter size of int array: 3 Please enter int value at index 0: 1 Please enter int value at index 1: 2 Please enter int value at index 2: 3 Enter array index to get the value: 4 Valid index range is from 0 to 2 Enter array index to get the value: -1 Valid index range is from 0 to 2 Enter array index to get the value: 2 Value at 2 = 3 |
So this is how we avoid getting ArrayIndexOutOfBoundsException in our program. That’s all for a quick roundup on java.lang.ArrayIndexOutOfBoundsException
.
Reference: API Doc