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
- All Java errors implement the
java.lang.Throwable
interface, or are extended from another inherited class therein. java.lang.Exception
inherits fromjava.lang.Throwable
.java.lang.RuntimeException
inherits fromjava.lang.Exception
.java.lang.IndexOutOfBoundsException
inherits fromjava.lang.RuntimeException
.- Lastly,
java.lang.ArrayIndexOutOfBoundsException
inherits fromjava.lang.IndexOutOfBoundsException
.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
package io.airbrake; import io.airbrake.utility.*; public class Main { public static void main(String[] args) { // Create list of Books. Book[] library = { new Book("The Pillars of the Earth", "Ken Follett", 973), new Book("Gone Girl", "Gillian Flynn", 555), new Book("His Dark Materials", "Philip Pullman", 399), new Book("Life of Pi", "Yann Martel", 460) }; // Iterate over array. iterateArray(library); Logging.lineSeparator(); iterateArrayInvalid(library); } /** * Iterate over passed Array, logging each element. * * @param list */ private static void iterateArray(Book[] list) { try { // Loop through each element by index. for (int index = 0; index < list.length; index++) { // Output element. Logging.log(list[index]); } } catch (ArrayIndexOutOfBoundsException exception) { // Output expected ArrayIndexOutOfBoundsException. Logging.log(exception); } catch (Exception exception) { // Output unexpected Exceptions. Logging.log(exception, false); } } /** * Iterate over passed Array, logging each element. * * For-loop limit includes list.length, which is invalid. * * @param list */ private static void iterateArrayInvalid(Book[] list) { try { // Loop through each element by index. // Less-than or equal to (<=) limit results in an exception. for (int index = 0; index <= list.length; index++) { // Output element. Logging.log(list[index]); } } catch (ArrayIndexOutOfBoundsException exception) { // Output expected ArrayIndexOutOfBoundsException. Logging.log(exception); } catch (Exception exception) { // Output unexpected Exceptions. Logging.log(exception, false); } } } // Book.java package io.airbrake; /** * Simple example class to store book instances. */ public class Book { private String author; private String title; private Integer pageCount; /** * Constructs an empty book. */ public Book() { } /** * Constructs a basic book. * * @param title Book title. * @param author Book author. */ public Book(String title, String author) { setAuthor(author); setTitle(title); } /** * Constructs a basic book, with page count. * * @param title Book title. * @param author Book author. * @param pageCount Book page count. */ public Book(String title, String author, Integer pageCount) { setAuthor(author); setPageCount(pageCount); setTitle(title); } /** * Get author of book. * * @return Author name. */ public String getAuthor() { return author; } /** * Get page count of book. * * @return Page count. */ public Integer getPageCount() { return pageCount; } /** * Get a formatted tagline with author, title, and page count. * * @return Formatted tagline. */ public String getTagline() { return String.format("'%s' by %s is %d pages.", this.title, this.author, this.pageCount); } /** * Get title of book. * * @return Title. */ public String getTitle() { return title; } /** * Set author of book. * * @param author Author name. */ public void setAuthor(String author) { this.author = author; } /** * Set page count of book. * * @param pageCount Page count. */ public void setPageCount(Integer pageCount) { this.pageCount = pageCount; } /** * Set title of book. * * @param title Title. */ public void setTitle(String title) { this.title = title; } /** * Throw an Exception. */ public void throwException(String message) throws Exception { throw new Exception(message); } } // Logging.java package io.airbrake.utility; import java.util.Arrays; import org.apache.commons.lang3.builder.*; /** * Houses all logging methods for various debug outputs. */ public class Logging { /** * Outputs any kind of Object. * Uses ReflectionToStringBuilder from Apache commons-lang library. * * @param value Object to be output. */ public static void log(Object value) { if (value instanceof String) { System.out.println(value); } else { System.out.println(new ReflectionToStringBuilder(value, ToStringStyle.MULTI_LINE_STYLE).toString()); } } /** * Outputs passed in Throwable exception or error instance. * Can be overloaded if expected parameter should be specified. * * @param throwable Throwable instance to output. */ public static void log(Throwable throwable) { // Invoke call with default expected value. log(throwable, true); } /** * Outputs passed in Throwable exception or error instance. * Includes Throwable class type, message, stack trace, and expectation status. * * @param throwable Throwable instance to output. * @param expected Determines if this Throwable was expected or not. */ public static void log(Throwable throwable, boolean expected) { System.out.println(String.format("[%s] %s", expected ? "EXPECTED" : "UNEXPECTED", throwable.toString())); throwable.printStackTrace(); } /** * Output a dashed line separator of default (40) length. */ public static void lineSeparator() { // Invoke default length method. lineSeparator(40); } /** * Output a dashed lin separator of desired length. * * @param length Length of line to be output. */ public static void lineSeparator(int length) { // Create new character array of proper length. char[] characters = new char[length]; // Fill each array element with character. Arrays.fill(characters, '-'); // Output line of characters. System.out.println(new String(characters)); } } |
To illustrate a common problem when using arrays we have two similar methods, iterateArray(Book[] list)
and iterateArrayInvalid(Book[] list)
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/** * Iterate over passed Array, logging each element. * * @param list */ private static void iterateArray(Book[] list) { try { // Loop through each element by index. for (int index = 0; index < list.length; index++) { // Output element. Logging.log(list[index]); } } catch (ArrayIndexOutOfBoundsException exception) { // Output expected ArrayIndexOutOfBoundsException. Logging.log(exception); } catch (Exception exception) { // Output unexpected Exceptions. Logging.log(exception, false); } } /** * Iterate over passed Array, logging each element. * * For-loop limit includes list.length, which is invalid. * * @param list */ private static void iterateArrayInvalid(Book[] list) { try { // Loop through each element by index. // Less-than or equal to (<=) limit results in an exception. for (int index = 0; index <= list.length; index++) { // Output element. Logging.log(list[index]); } } catch (ArrayIndexOutOfBoundsException exception) { // Output expected ArrayIndexOutOfBoundsException. Logging.log(exception); } catch (Exception exception) { // Output unexpected Exceptions. Logging.log(exception, false); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static void main(String[] args) { // Create list of Books. Book[] library = { new Book("The Pillars of the Earth", "Ken Follett", 973), new Book("Gone Girl", "Gillian Flynn", 555), new Book("His Dark Materials", "Philip Pullman", 399), new Book("Life of Pi", "Yann Martel", 460) }; // Iterate over array. iterateArray(library); Logging.lineSeparator(); iterateArrayInvalid(library); } |
The output from iterateArray(Book[] list)
is just as expected, outputting all four elements before execution is completed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
io.airbrake.Book@532760d8[ author=Ken Follett title=The Pillars of the Earth pageCount=973 ] io.airbrake.Book@5679c6c6[ author=Gillian Flynn title=Gone Girl pageCount=555 ] io.airbrake.Book@27ddd392[ author=Philip Pullman title=His Dark Materials pageCount=399 ] io.airbrake.Book@19e1023e[ author=Yann Martel title=Life of Pi pageCount=460 ] |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
io.airbrake.Book@532760d8[ author=Ken Follett title=The Pillars of the Earth pageCount=973 ] io.airbrake.Book@5679c6c6[ author=Gillian Flynn title=Gone Girl pageCount=555 ] io.airbrake.Book@27ddd392[ author=Philip Pullman title=His Dark Materials pageCount=399 ] io.airbrake.Book@19e1023e[ author=Yann Martel title=Life of Pi pageCount=460 ] [EXPECTED] java.lang.ArrayIndexOutOfBoundsException: 4 |
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!