https://kotlinlang.org logo
#coroutines
Title
# coroutines
o

orafaaraujo

01/21/2020, 1:07 PM
Hi, all the
.catch { }
operator of
Flow
should not get one
NPE
that might occurs inside it?
d

Dico

01/21/2020, 1:10 PM
Please provide example code, preferably using play.kotlinlang.org, to demonstrate the observation.
👍 1
o

orafaaraujo

01/21/2020, 1:12 PM
Will do it While that, I'm getting a NPE and my catch is not getting it
.catch { e -> emit(Result.Error(Exception(e))) }
I'm sorry but how can I test Flow in the playground ?
I'm mocking my scenario but when I do it, the
catch
works 😞
Is basically
Copy code
val myFlow = callbackFlow {
        val json: String? = "aaa"
        val something = json
            ?.let { mapJson("") }
            ?: run { listOf<Any>() }
        offer(something)
    }

    myFlow
        .catch {
            logD { "myFlow catch $it" }
            emit(listOf())
        }
        .collect { logD { "myFlow collect: $it" } }
}
but here I can get a
NPE
when the map is wrong because of a bad JSON received
d

Dico

01/21/2020, 2:18 PM
then you're making an incorrect assumption somewhere
l

louiscad

01/21/2020, 2:37 PM
@orafaaraujo You can manually import
kotlinx.coroutines.*
and
kotlinx.coroutines.flow.*
on play.kotl.in
🎉 2
o

orafaaraujo

01/21/2020, 2:52 PM
After hours of investigation, I discovered that exceptions launched from inside another lambda (from a library that I'm using here) are not handled by
catch
why? no idea 😂
Copy code
throw NullPointerException() // handled by catch
val request = searcher.searchable.searchAsync(query) { jsonObject, exception ->
    throw NullPointerException() // not handled by catch
}
I believe is the same reason why I need to use
callbackFlow
instead only
Flow
, to handle with something inside lambdas
d

dave08

01/21/2020, 5:20 PM
If the exception is in a callback you're wrapping and the callback is on another thread, I don't think it'll be handled by catch...
e

elizarov

01/22/2020, 7:23 AM
It depends on if/how
searchAsync { … }
handles exception. The root cause if there.
👍 1
3 Views