Hello Guys, The following code throws IntrruptedEx...
# coroutines
p
Hello Guys, The following code throws IntrruptedException,Is there a way where it dosen’t do anything ? Should i just catch it and return a random value if method has a return type?
Copy code
fun getPreferenceLong(key: String, defaultValue: Long): Long {
    return runBlocking {
        context.dataStore.data.map {
            it[longPreferencesKey(key)] ?: defaultValue
        }.first()
    }
}
This mostly throws the exception when called from a RxJava Code , for now can’t make it return Flow as it has usages in legacy codebase as well
s
InterruptedException
isn’t directly related to coroutines: it’s linked to the fact that you’re blocking a thread. If
runBlocking
throws
InterruptedException
, it means something interrupted the calling thread while it was blocked. This typically means that the application is attempting to stop the thread and discard its work. The proper way to handle it is generally to allow the exception to propagate so that the thread does indeed terminate.
This StackOverflow thread has some decent suggestions: https://stackoverflow.com/questions/3976344/handling-interruptedexception-in-java
p
The StackOverflow suggestions are very clear about what needs to be done. Thanks a lot🙌