About onErrorReturn
onErrorReturn
is an rxjava operator that enable emit a placeholder value instead of emit an error
Now let’s try to find what will happen after using this operator
Trial Code
Take a look at code snippet below
import io.reactivex.rxjava3.core.Observable
fun main() {
Observable.just(Unit)
.doOnNext { println("next 1") }
.doOnError { println("error 1") }
.map<String> { throw RuntimeException("error") }
.doOnNext { println("next 2") }
.doOnError { println("error 2") }
.onErrorReturn { "error placeholder 3" }
.doOnNext { println("next 3") }
.doOnError { println("error 3") }
.doOnComplete { println("complete 4") }
.doFinally { println("finally 4") }
.subscribe()
}
Console Outputs
This is the result of execution above
1
4
5
7
9
Summary
We can take a conclusion of those
doOnError
beforeonErrorReturn
will catch errordoOnError
afteronErrorReturn
will not catch errordoOnNext
will receive the placeholder valuedoOnComplete
anddoFinally
will be called, means the whole process is regard as successfulonErrorReturn
can only replace error occurs above, which is not tested here
Bless
Good Work, Good Study, Good Progress, and Good Mood !