When I ran the following code
Caught expected ConcurrentModificationException
is printed which is expected but strange thing is on second iteration no exception is being caught and
Failed to catch expected ConcurrentModificationException
is printed. I am really not sure why for second time it is not caught.
public class TestLHS {
public static void main(String[] args) {
LinkedHashSet<Integer> lhSet = new LinkedHashSet<Integer>();
Integer one = new Integer(1);
Integer two = new Integer(2);
Integer three = new Integer(3);
Integer four = new Integer(4);
Integer cinco = new Integer(5);
// Add the three objects to the LinkedHashSet.
// By its nature, the LinkedHashSet will iterate in
// order of insertion.
lhSet.add(one);
lhSet.add(two);
lhSet.add(three);
// 1. Iterate over set. try to insert while processing the
// second item. This should throw a ConcurrentModificationEx
try {
for (Iterator<Integer> it = lhSet.iterator(); it.hasNext();) {
Integer num = (Integer) it.next();
if (num == two) {
lhSet.add(four);
}
System.out.println(num);
}
} catch (ConcurrentModificationException ex) {
System.out.println("Caught expected ConcurrentModificationException");
}
// 2. Iterate again, this time inserting on the (old) 'last'
// element. This too should throw a ConcurrentModificationEx.
// But it doesn't.
try {
for (Iterator<Integer> it = lhSet.iterator(); it.hasNext();) {
Integer num = (Integer) it.next();
if (num == four) {
lhSet.add(cinco);
}
System.out.println(num);
}
System.out.println("Failed to catch expected ConcurrentModificationException");
} catch (ConcurrentModificationException ex) {
System.out.println("Caught expected ConcurrentModificationException");
}
}
}
Can someone explain this behavior?