Help needed on try/catch inside for loop

I created my own exception class InvalidHallOfFameIDException. Basically what i’m doing is looping through a arraylist called hallOfFameMemberList that holds hall of fame members. Inside that loop i am looping through Hall Of Fames, which members can belong to. An exception(InvalidHallOfFameIDException) is thrown when a member’s id from hallOfFameMemberList does not equal to the Hall Of Fame’s id. The problem I am having is after the exception is thrown. After the exception is thrown I want to continue looping through hallOfFameMemberList. It goes to the next hofMember of hallOfFameMemberList then gives me this error:

java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at ReportWriter.displayReport3(HW6.java:1708)
at ReportWriter.DisplayReports(HW6.java:1478)
at HW6.main(HW6.java:399)

Here is my code


		/*loop to check and delete member from arraylist if member hall of fame id does not equal to hall of fame id*/
				for(HallOfFameMember hofMember: hallOfFameMemberList){
					
					memberFound = false;
					
					
					try{
						
						for(HallOfFame hof:hallOfFameArr){
						
						
								if(hofMember.GetHallOfFameId() == hof.GetHallOfFameId()){
							
                                                                     /*do something then set memberFound = true*/
							
								}
							
						}
					
						if(memberFound == false){
						

                                                        /*remove member from list*/
							hallOfFameMemberList.remove(hofMember);
							throw new InvalidHallOfFameIDException();
						}
					
					}catch(InvalidHallOfFameIDException e){
						

                                                hallOfFameMemberList.remove(hofMember);
						System.err.println("InvalidHallOfFameIDException was thrown:");
			                        System.err.println("HallOfFameMember " + hofMember.GetLastName() + ", " + hofMember.GetFirstName() + " with HOFId " + hofMember.GetHallOfFameId() + " was deleted");
						
						}


I don’t quite understand the error. Any ideas on what I can do?

java.util.ConcurrentModificationException
means you are modifying a collection (e.g. a List) while you are iterating over it.

In this case you’re iterating over “hallOfFameMemberList”, but you’re also trying to remove from it while you are still looping.

Instead I’d make a separate variable, e.g. validHallOfFameMemberList, and add all the good HallOfFameMembers there.

Jurn is right…you need to use an another list for storing valid members.

Also give a thought to BlockingQueues introduced in JDK5 and especially [URL=“http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/LinkedBlockingQueue.html”]LinkedBlockingQueue

Thanks for the reply guys!

I replaced the for(HallOfFameMember hofMember: hallOfFameMemberList) with a for loop to loop backwards so the next index being looped is never shifted. :slight_smile:

I appreciate you help.

Another valid solution is to use the standard for, which gives you an iterator to work with.

Via the iterator, even during iteration, you may remove elements.

Like so:


    // where myStuff [i]is a[/i] Collection
    for( Iterator<Stuff> i = myStuff.iterator(); i.hasNext(); ) {
        Stuff stuff = i.next();
        if( iDontWantThisStuff( stuff ) ) {
            i.remove();
        }
    }