Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatedly thrown ArrayIndexOutOfBoundsException stop producing stack traces [duplicate]

I'm writing tests for my program. Sometimes my tests work, and sometimes they fail. I'm trying to track down all of the sources of nondeterminism.

Here's a simple, self-contained example of my problem:

import java.util.ArrayList;

public class ArrayExceptions {
    public static void main(String[] args) {
        final int ITERATIONS = 10000;
        ArrayList<Integer> list = new ArrayList<>();
        try {
            list.get(-1);
        } catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
        }
        for(int i = 0; i < ITERATIONS; i++) {
            try{
                list.get(-1);
            } catch (ArrayIndexOutOfBoundsException e) {
                if(e.getMessage() == null) {
                    System.out.println(i);
                    break;
                }
            }
        }
        for(int i = 0; i < 10; i++) {
            try {
                list.get(-1);
            } catch(ArrayIndexOutOfBoundsException e) {
                e.printStackTrace();
            }
        }
    }
}

This program creates an ArrayList. It tries to access element -1 repeatedly. Initially, this creates a detailed backtrace and a message alongside the ArrayIndexOutOfBoundsException. However, if you call it enough times (where 'enough' seems to be about 5500) the thrown exception lacks a backtrace and a message.

The output of this program varies from run to run. Here it exits early, at 5494 iterations:

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:418)
    at java.util.ArrayList.get(ArrayList.java:431)
    at ArrayExceptions.main(ArrayExceptions.java:8)
5494
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException

Here it fails late, at 5540 iterations:

java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.ArrayList.elementData(ArrayList.java:418)
    at java.util.ArrayList.get(ArrayList.java:431)
    at ArrayExceptions.main(ArrayExceptions.java:8)
5540
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException

Why's it doing this?

like image 907
Nick ODell Avatar asked Oct 18 '17 23:10

Nick ODell


People also ask

How do I fix ArrayIndexOutOfBoundsException?

In order to avoid the java. lang. ArrayIndexOutOfBoundsException, you should always do the bound check before accessing array element e.g. Always remember that the array index starts at 0 and not 1 and an empty array has no element in it.

What causes ArrayIndexOutOfBoundsException?

What Causes ArrayIndexOutOfBoundsException. The ArrayIndexOutOfBoundsException is one of the most common errors in Java. It occurs when a program attempts to access an invalid index in an array i.e. an index that is less than 0, or equal to or greater than the length of the array.

What type of error is ArrayIndexOutOfBoundsException?

The ArrayIndexOutOfBoundsException is a Runtime Exception thrown only at runtime. The Java Compiler does not check for this error during the compilation of a program.

What does ArrayIndexOutOfBoundsException mean?

ArrayIndexOutOfBoundsException means you are trying to access a array index that doesn't exist. The problem is that your array is of size one.


1 Answers

Nevermind, figured it out.

Pass -XX:-OmitStackTraceInFastThrow as a command line option to the JVM to turn this behavior off. See also NullPointerException stack trace not available without debug agent

like image 76
Nick ODell Avatar answered Oct 12 '22 22:10

Nick ODell