What is the correct way of catching an exception i...
# coroutines
c
What is the correct way of catching an exception in
Flow.flatMapMerge
? I have this:
Copy code
val f: Flow<CustomObject> = ...
f.flatMapMerge {
  it.customFlow()
}
This works fine as long as there is no exception, however if there is one, the entire flow is cancelled. I tried to write
Copy code
f.flatMapMerge {
  try {
    it.customFlow()
  } catch(e: Throwable) {
    println("Caught")
    emptyFlow()
}
} but that still cancels the flow, instead of just skipping items for which the conversion fails.
Oh, that's because the exception is only thrown during collection. But if I add a
.catch
after the
.flatMapMerge
, the flow has still been cancelled.
z
Use the catch operator on the inner flow (customFlow)
c
Thanks! Not sure how I missed that 😓