ArrayIndexOutOfBoundsException is the exception automatically thrown by the Java Runtime Environment when a Java program incorrectly attempts to access a location in an array that does not exist. This can happen when the requested array index is negative, or greater than or equal to the size of the array. (Java arrays use zero-based indexing, so the first element of the array has index zero, the last element has index size-1, and the nth element in general has index n-1.)

Array bounds checking such as this prevents segmentation faults. Because Java is a bytecode-interpreted language, the default error message printed when this error occurs includes the line number and the invalid index itself that induced the exception. This makes such failures relatively easy to debug.

There is a downside to all this error checking. Every array access has a hidden comparison operation within it, when the computer checks that the index is within the range of the array. Although the Java just-in-time compilation model can sometimes allow the computer to skip this step, this checking can slow the program down. Whether or not this is significant depends upon what the program is doing. If the program is an online shopping cart or a Java-based chat client, the slowdown is irrelevant. If the program is attempting to do really complicated mathematics involving billions and billions of operations on arrays, the slowdown is likely to be very prominent.

This error checking is an example of why not all programming languages are suited to all tasks. Although Java would be a poor choice for the aforementioned number cruncher, its rigorous protection and portability make it the best choice for many Internet-based uses. C++, conversely, can do mathematics very quickly, but it must be recompiled for each processor it is expected to execute on, and is inherently capable of damaging a system with improper use of pointers in ways that make it unsafe for being embedded into a website in the way that Java applets are.

ArrayIndexOutOfBoundsException inherits from RuntimeException and is therefore unchecked. A program is not expected to catch it, and like all uncaught exceptions in Java, it will crash the program if it is not caught. It is usually correct to leave this exception uncaught, as no array index should ever go out of bounds within the normal execution of a correctly-written program.

The presence of the ArrayIndexOutOfBoundsException is one of the reasons that Java is a popular language to teach in introductory computer science courses. It, like many other Java exceptions and constructs, prevents the programmer from shooting himself, herself, itself, or xirself in the foot. This makes it much easier for a novice programmer to debug code and learn what not to do in the first place. The danger, of course, is reliance on the error, so that when the programmer moves to a language that does not have this safeguard, such as C or C++, the programmer might be much more likely to create segmentation faults.

Log in or register to write something here or to contact authors.