February 2020
Intermediate to advanced
412 pages
9h 36m
English
When you want to resort to a default value when an exception occurs, you can use the onErrorReturnItem() operator. If we want to emit -1 when an exception occurs, we can do it like this:
import io.reactivex.rxjava3.core.Observable;public class Ch3_42 { public static void main(String[] args) { Observable.just(5, 2, 4, 0, 3) .map(i -> 10 / i) .onErrorReturnItem(-1) .subscribe(i -> System.out.println("RECEIVED: " + i), e -> System.out.println("RECEIVED ERROR: " + e)); }}
The output of the preceding code snippet is as follows:
RECEIVED: 2RECEIVED: 5RECEIVED: 2RECEIVED: -1
You can see that the emissions stopped after the error anyway, but the error itself did not flow down to the Observer. Instead, ...
Read now
Unlock full access