2017-10-18 4 views
5

J'écris des tests pour mon programme. Parfois, mes tests fonctionnent, et parfois ils échouent. J'essaie de retrouver toutes les sources du non-déterminisme.ArrayIndexOutOfBoundsException levé à plusieurs reprises arrêter de produire des traces de pile

Voici un exemple simple et autonome de mon problème:

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(); 
      } 
     } 
    } 
} 

Ce programme crée un ArrayList. Il essaie d'accéder à l'élément -1 à plusieurs reprises. Initialement, cela crée un backtrace détaillé et un message à côté de l'exception ArrayIndexOutOfBoundsException. Cependant, si vous l'appelez assez souvent (où «assez» semble être environ 5500), l'exception lancée n'a pas de trace et de message.

La sortie de ce programme varie d'un cycle à l'autre. Ici, il sort tôt, à 5494 itérations:

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 

Ici, il échoue tard, à 5540 itérations:

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 

Pourquoi fait-il cela?

Répondre