Catch them all!

Report a typo

We have a dangerous flow we need to process: if it throws an exception, we just want to stop safely. But if it keeps flowing, we need to process the data with a function that can also fail: if that happens, we should log the failure information.

Add error handling so that an exception from the dangerous flow is ignored but an exception from the `process` operation is logged at completion before being re-thrown.

Tip: An error in `onCompletion` can be null

Write a program in Kotlin
fun registerError(error: Throwable) {
log("Finished with an error ${error.message}")
}
// add error handling without changing existing operators
// no need to do anything with the error from the flow, ignore it
// register and error that may happen on processing with `registerError`
dangerousFlow
.map { data -> process(data) }
.collect { data -> store(data) }
___

Create a free account to access the full topic