I have a requirement in our code, where we need to catch ArrayIndexOutOfBoundsException and move forward with other elements.
However, when i try to do that i am getting ConcurrentModificationException
Following code is something that i want but it is throwing ConcurrentModificationException exception at line #1
Note that list.remove(1000) is not causing any issue but list.remove(-1) does
public class Test1 {
public static void main(String[] argv) {
test(new ArrayList<>());
}
public static void test(List<Integer> list) {
list.add(42);
Iterator<Integer> it = list.iterator();
try {
list.remove(-1);
} catch (Exception e) {
System.out.println(e);
}
try {
if (it.next() != 42);//Line #1
} catch (Exception e) {
System.out.println(e);
}
}
}
Can some one help me understand this behavior and how can I fix this problem ?