我有一个流程可能会引发如下错误:
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
}
我的问题是,即使我添加了.catch { error -> emit("5") }
.. 也会引发错误,它仍然会完成流程,因此不会"6"
发出。
myFlow.catch { error ->
emit("5")
}.onEach {
println("$it")
}.onCompletion {
println("Complete")
}.launchIn(scope)
结果是:
1
2
3
4
5
Complete
我需要它是:
1
2
3
4
5
6
Complete
我想吞下错误而不是完成流程。我怎样才能做到这一点?