Есть ли способ заменить ArrayIndexOutOfBoundsException, вызывая значение некоторого значения по умолчанию? - PullRequest
0 голосов
/ 12 ноября 2019

Я пытаюсь заменить значение, вызывающее ArrayIndexOutOfBounds, на 0.

Вывод, который я надеюсь получить:

0
10
20
30
40
0
0
0
0
0

Есть ли способ добиться этого?

Обратите внимание, что я не хочу этого для печати (что я мог бы сделать, выполнив System.out.println ("0") в блоке catch.

public class test {
  int[] array;

  @Test
  public void test() {
    array = new int[5];
    for (int i = 0; i < 5; i++) {
      array[i] = i;
    }

    for(int i = 0; i < 10; i++) {
      try {
        System.out.println(array[i] * 10);
      }
      catch(ArrayIndexOutOfBoundsException e) {
        //code to replace array[i] that caused the exception to 0
      }
    }
  }
}

...