Andrey Tabakov
07/27/2023, 3:43 PMflow {
emit(0)
emit(1)
emit(2)
throw IllegalStateException("some error")
emit(3)
emit(4)
}
.catch { e -> println(e)}
.onCompletion { println("complete") }
.launchIn(CoroutineScope(Dispatchers.Default))
Hello, can someone explain is there a way to handle an exception without try-catch code. Now, this exception causes onCompletion
, but I want to continue working with dataFlow even if an exception appears.kevin.cianfarini
07/27/2023, 3:47 PMflow {
emit(0)
emit(1)
emit(2)
try { throw IllegalStateException("some error") } catch (e: IllegalStateException) { /* suppress */ }
emit(3)
emit(4)
}
If it only happens intermittently, there’s Flow.retry
Andrey Tabakov
07/27/2023, 3:48 PMkevin.cianfarini
07/27/2023, 3:49 PMAndrey Tabakov
07/27/2023, 3:59 PMkevin.cianfarini
07/27/2023, 4:08 PMfranztesca
07/27/2023, 4:08 PMkevin.cianfarini
07/27/2023, 4:09 PMfun complexLogic(logger: Logger) = try {
complex()
} catch(e: SomeException) {
logger.log(e)
}
kevin.cianfarini
07/27/2023, 4:10 PMRobert Williams
07/27/2023, 4:11 PMfranztesca
07/27/2023, 4:14 PMRobert Williams
07/27/2023, 4:18 PMkevin.cianfarini
07/27/2023, 4:39 PMmapWithTryCatch
will only catch exception from the lamba action
you pass in; it won’t catch upstream exceptions.Andrey Tabakov
07/27/2023, 4:39 PMPatrick Steiger
07/30/2023, 2:19 AMouterFlow
.flatMapLatest {
innerFlowThatThrows
.catch { something }
}
Outer flow is still alive after inner flow throws, and restarts inner flow after every new emission