Ircover
Ircover

Reputation: 2446

Android try/catch didn't work

There is system in my Android app for sending reports about uncaught exceptions (google play account doesn't belong to me). Some days ago I've got following error:

java.util.ConcurrentModificationException
java.util.ArrayList$ArrayListIterator.next(ArrayList.java:569)
onlineorganizer.Adapter.fillGrid(Adapter.java:332)
onlineorganizer.Adapter.access$6(Adapter.java:314)
onlineorganizer.Adapter$1.run(Adapter.java:131)
java.lang.Thread.run(Thread.java:856)

It appears inside following method:

private void fillGrid(GridLayout layout) {
    grid = layout;
    Iterator<View> iter = null;
    boolean isCellsEmpty;
    if(cells == null) {
        cells = new LinkedList<View>();
        isCellsEmpty = true;
    } else {
        iter = cells.iterator();
        isCellsEmpty = false;
    }
    if(dates != null) {
        for (final String date : dates) {
            JSONObject event = events.get(date);
            View view;
            if(isCellsEmpty) {
                view = null;
            } else {
                if (iter.hasNext())
                    try {
                        // HERE
                        view = iter.next(); //exception appears HERE
                        // HERE
                    }
                    catch (ConcurrentModificationException e) {
                        Log.e("next in list", e.toString());
                        return;
                    }
                else
                    view = null;
            }
            if (event!=null && event.has("evid")) {
                view = getEventView(date, view);
            } else {
                view = getEmptyView(date, view);
            }
            if(isCellsEmpty && cells != null) {
                cells.add(view);
            }
        }
    }
}

As you can see, exception appears inside try/catch block, that should handle it. And now you can guess what question I have.

Why catch block didn't catch exception?

Upvotes: 0

Views: 917

Answers (2)

Satyavrat
Satyavrat

Reputation: 469

your condition :
if (iter.hasNext())
is out of try block put that in try block then check it .hope it will help you :)

Upvotes: 0

Gunnar Karlsson
Gunnar Karlsson

Reputation: 28480

You need to eliminate the reason the issue is happening. You can synchronize access to the datastructure or use a thread safe datastructure.

See for example:

TryCatch ConcurrentModificationException catching `30% of the time

http://nerddawg.blogspot.hk/2004/09/concurrent-modification-of-collections.html

Upvotes: 1

Related Questions