The onErrorResumeNext operator
The onErrorResumeNext
operator helps you subscribe to a different producer in case any error occurs.
Here is an example:
fun main(args: Array<String>) { Observable.just(1,2,3,4,5) .map { it/(3-it) } .onErrorResumeNext(Observable.range(10,5))//(1) .subscribe { println("Received $it") } }
The output is as follows:
This operator is especially useful when you want to subscribe to another source producer in case any error occurs.
Retrying on error
The retry
operator is another error handling operator that enables you to retry/re-subscribe to the same producer when an error occurs. You just need to provide a predicate or retry-limit when it should stop retrying. So, let's look at an example:
fun main(args: Array<String>) { Observable.just(1,2,3,4,5) .map { it/(3-it) } .retry(3)//(1) .subscribeBy ( onNext = {println("Received $it")}, ...