0

I am using a library that sometimes (about 1 of 100 times) throws a ConcurrentModificationException leading to an app crash.

The stacktrace does not mention any code of mine but instead the code of the library. I tried to catch the exception where I call the library however it does not work.

I guess the exception is thrown in a new thread within the library.

How could I catch such an exception?

Libary: Old Philips Hue Java library. (I am going to switch to the new one, soon.)

Exception:

E/AndroidRuntime: FATAL EXCEPTION: Thread-21319
    Process: my.package, PID: 21561
    Theme: themes:{...}
    java.util.ConcurrentModificationException
        at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
        at com.philips.lighting.hue.sdk.notification.impl.PHNotificationManagerImpl$3.onReceived(PHNotificationManagerImpl.java:180)
        at com.philips.lighting.hue.sdk.notification.impl.PHHueResultReceiver.execute(PHHueResultReceiver.java:24)
        at com.philips.lighting.hue.sdk.notification.impl.PHNotificationManagerImpl.notifyBridgeSearchResult(PHNotificationManagerImpl.java:188)
        at com.philips.lighting.hue.sdk.upnp.PHBridgeSearchManagerImpl$1.run(PHBridgeSearchManagerImpl.java:198)
  • 1
    You should add more information, such as the stacktrace and which library you're using. – advice Dec 18 '18 at 21:42
  • Stacktrace is added. However, I just want to know whether there is an option to catch such an exception. – Julian Eggers Dec 18 '18 at 21:46
  • I may be wrong, but I don't think so. Because it's not your thread, you can't really wrap it in any `try/catch`. Maybe there is something else you can do, but I don't think so. I'd look more into the library, see if that's common. Do you happen to control when the threads are fired off? If so, maybe then you could prevent this. – advice Dec 18 '18 at 21:49
  • Is there a chance you could pass a threadsafe collection to the library (see this answer https://stackoverflow.com/questions/11360401/java-synchronized-list)? As others have said, I don't believe you can handle exceptions raised in other threads. – Eric Dec 18 '18 at 21:58
  • The library is really bad imo and I am going to switch to another one soon. I cannot control anything concerning the threads. They are created somewhere within the library. – Julian Eggers Dec 18 '18 at 21:58
  • @Eric The library is handling all that without letting me do anything. – Julian Eggers Dec 18 '18 at 22:04

1 Answers1

0

I guess you modify array value while you iterating throu it.

Java Collection classes are fail-fast, which means if the Collection will be changed while some thread is traversing over it using iterator, the iterator.next() will throw ConcurrentModificationException.

Here is how to avoid it : How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList

YouneS
  • 390
  • 4
  • 10