Maciek
12/22/2021, 11:50 AMcatch { } in the chain? If I'd want to have a infinite flow I must handle the exceptions with try catch in the lambdas because flow will close after any exception "leaking" to the stream, right? Or is there a way to do some explicit recover from the exception? Example code, only 0 will be emitted, next emit 1 is lost due to flow completion after exception.
flow {
emit(0)
emit(1) // this emit is never reached
}
.onEach { if (it == 0) throw Throwable() }
.catch { println(it) }
.collect()Maciek
12/22/2021, 11:51 AMtry catch in the onEachelizarov
12/22/2021, 4:08 PMcatch lets you handle the flow exception, but there’s no way to recover from it the way you want. The flow is already dead.elizarov
12/22/2021, 4:09 PMemit(0) had already threw an exception and emit(1) cannot be ever reached.Maciek
12/22/2021, 4:12 PMMaciek
12/22/2021, 4:24 PMNick Allen
12/22/2021, 5:07 PMNick Allen
12/22/2021, 5:14 PMFlow is required to not emit anything else. This is how operators like first and take work, they throw exceptions inside collect and then catch only their own exceptions outside collect.Maciek
12/22/2021, 5:18 PMexpensivebelly
12/23/2021, 2:54 PMretryWhen to recover from Exceptions, once it goes past retryWhen (if the predicate returns false) then at that point the Flow has completed with an exception (as far as I know) and there is no way to re-subscribe to it