java.lang.ArrayIndexOutOfBoundsException

Filed Under: Java

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

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.
ArrayIndexOutOfBoundsException class diagram

java.lang.ArrayIndexOutOfBoundsException Example

Let’s look at a simple example where our program may throw ArrayIndexOutOfBoundsException based on user input.


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.


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.


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.


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

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages