I agree that most of the time these things are easy to track down, but
sometimes they aren't, for example with multiple threads. If you have
a thread accessing the list (removing things from the list, for
whatever reason), then the ConcurrentModificationException is raised
in the other thread. You have no idea which other thread has modified
the list under what circumstances.

Please note that I said 'badly written'. I'm not arguing that a
properly written thread safe application would have problems with
this.

I would say that if the initial list is immutable (or unmodifiable)
this has two consequences:

1) The developer using the method that returns the list would have to
ask himself whether or not he really needs to modify this list (or
make a defensive copy himself).
2) If the developer doesn't have that reflection, it doesn't matter,
he gets an exception if he tries. :-)

Personally, I think it's good defensive coding.

Cordialement,

Matthew.

2011/3/30 Reinier Zwitserloot <[email protected]>:
> On Wednesday, March 30, 2011 8:41:24 AM UTC+2, Matthew Farwell wrote:
>>
>> ConcurrentModificationExceptions. You can come across this sort of
>> thing in web servers, admittedly ones badly written.
>>
>> This sort of bug is seemingly random and very hard to track down.
>
> Huh? No it isn't.
> ConcurrentModificationException has very little to do with 'concurrent' in
> the sense of threading, i.e. the way java.util.concurrent uses it. CoModExes
> are not at all hard to track down. You modify a collection in the middle of
> iterating over it and you get one. Every time. Reliable as clockwork. The
> exception will occur at the point of the loop (where .hasNext() is called),
> instead of where you actually modify it, but in the vast majority of these
> cases its easy to find where inside the loop the list is modified.

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to