Java Exception Handling

Java Exception Handling – java.lang.ArrayIndexOutOfBoundsException

Today we’ll take another journey through the “Land of Errors” of our ongoing Java Exception Handling series, with a deep dive into the java.lang.ArrayIndexOutOfBoundsException. As the name clearly indicates, the ArrayIndexOutOfBoundsException is thrown when an index is passed to an array which doesn’t contain an element at that particular index location.

In this article we’ll look a bit closer at the java.lang.ArrayIndexOutOfBoundsException by examining where it sits in the Java Exception Hierarchy. We’ll also go over a few simple, functional code samples illustrating how ArrayIndexOutOfBoundsExceptions are commonly thrown, so let’s get crackin’!

The Technical Rundown

When Should You Use It?

Since Java has internal classes and object structures to manage Arrays — and because said objects will produce errors like the java.lang.ArrayIndexOutOfBoundsException on their own — there will rarely be a situation where you’ll need to explicitly throw your own ArrayIndexOutOfBoundsException. For example, if you were creating your own data structure object that contained a non-array collection of elements, you’d likely want to explicitly throw a java.lang.IndexOutOfBoundsException, as opposed to a java.lang.ArrayIndexOutOfBoundsException, since the JVM will handle that for you most of the time.

That said, to see how ArrayIndexOutOfBoundsExceptions are commonly thrown we’ll start with the full working code sample, after which we’ll explore it in more detail:

To illustrate a common problem when using arrays we have two similar methods, iterateArray(Book[] list) and iterateArrayInvalid(Book[] list):

These methods don’t do anything fancy and, in fact, basically only serve as wrappers to stick our try-catch blocks in, and to differentiate the slight differences in loop logic between the two. Specifically, as indicated by the code comments, the iterateArrayInvalid(Book[] list) method contains a termination expression that allows index to be less than or equal to the length of list. Since, like most languages, Java uses zero-based numbering to index Arrays and other collections, an index equal to the length of an array will be one greater than the largest index. To better illustrate, consider this table of Arrays showing each array’s length and its maximum index:

Length/Count Maximum Index
1 0
2 1
3 2
4 3
5 4
etc. etc.

With that in mind, we’ll get started testing both of the iteration methods. We’ll use our Book class just to keep things a little more interesting by creating a few elements for our array, then pass it to both methods so we can review the output of each:

The output from iterateArray(Book[] list) is just as expected, outputting all four elements before execution is completed:

On the other hand, invoking iterateArrayInvalid(Book[] list) runs into a problem. While we successfully output all four elements, as mentioned above, the for loop iterates one too many times, resulting in a call to list[4], which is an unknown index. This throws a java.lang.ArrayIndexOutOfBoundsException, indicating the index value that was out of bounds:

The Airbrake-Java library provides real-time error monitoring and automatic exception reporting for all your Java-based projects. Tight integration with Airbrake’s state of the art web dashboard ensures that Airbrake-Java gives you round-the-clock status updates on your application’s health and error rates. Airbrake-Java easily integrates with all the latest Java frameworks and platforms like Spring, Maven, log4j, Struts, Kotlin, Grails, Groovy, and many more. Plus, Airbrake-Java allows you to easily customize exception parameters and gives you full, configurable filter capabilities so you only gather the errors that matter most.

Check out all the amazing features Airbrake-Java has to offer and see for yourself why so many of the world’s best engineering teams are using Airbrake to revolutionize their exception handling practices!