Unchecked Exceptions

Unchecked exceptions usually report a programming mistake or an invalid precondition: a null dereference, a bad array index, or malformed numeric text.

The compiler does not force callers to catch RuntimeException subclasses. Code should prevent these failures where practical instead of wrapping every statement in catch blocks.

A boundary failure

public class ArrayFailureProbe {
    public static void main(String[] args) {
        int[] scores = {91, 84, 77};
        System.out.println(scores[3]);
    }
}

Java reports ArrayIndexOutOfBoundsException. The captured message says that index 3 is outside an array of length 3, and its application frame points to line 4. The array is valid. The bug is the assumption that index 3 names its third element. Since indexing begins at zero, the third element is at index 2.

Other common unchecked exceptions include:

ExceptionTypical cause
NullPointerExceptionCalling a method through a null reference
NumberFormatExceptionParsing text that is not a valid number
IllegalArgumentExceptionPassing a value outside a method's contract

A length-three array stops at index 2. Reading index 3 crosses the boundary and throws ArrayIndexOutOfBoundsException.

Validate at meaningful boundaries, such as user input or a public method. Internal bugs should remain visible during development.

Identify the unchecked array failure in the question below.