I have a flow which could possible throw an error like so:
val myFlow = flow {
emit("1")
delay(2000)
emit("2")
delay(2000)
emit("3")
delay(2000)
emit("4")
delay(2000)
throw Exception() // here it would throw an error
delay(10000)
emit("6") // because the flow completes on error, it doesn't emit this
}
My problem is that, when the error is thrown even when I add a .catch { error -> emit("5") }
.. it still completes the flow, and so "6"
isnt emitted.
myFlow.catch { error ->
emit("5")
}.onEach {
println("$it")
}.onCompletion {
println("Complete")
}.launchIn(scope)
And the result is:
1
2
3
4
5
Complete
I need it to be:
1
2
3
4
5
6
Complete
I want to swallow the error instead of making the flow complete. How can I achieve this?
try { throw Exception() } catch(e: Exception) { emit("5") }
– Hogback