RxJava FlatMap: how to skip errors?

In the chain obs1.flatmap(x -> obs2()).subscribe(sub) , if obs2 causes an error, it raises an onError immediately on sub . This is documented:

Note that if any of the individual Observables are mapped to elements from the source. Surveillance using flatMap is interrupted by raising an onError. The manufacturer's observed flatMap will itself be canceled immediately and invoke onError.

But is it possible to ignore obs2 errors and make obs1 continuation of the emission?

+7
java rx-java
source share
3 answers

Rx provides some operators with the ability to make mistakes. Just create a third Observable from obs2 that does not propagate the error.

 Observable<YourType> obs3 = obs2.onErrorResumeNext(Observable.<YourType>empty()); obs1.flatmap(x -> obs3) 
+6
source share

.onErrorResumeNext - you can use this to try to handle it differently, this way you will pass the previously emitted value, which when processing generated an error for another observable, where you can try a different approach. Or handle the error.

.onErrorReturn - If this is normal for you, just return some default value and ignore the error.

+2
source share

Could you just make a big try-catch blog inside obs2() and leave the catch blog empty? There are no errors, and so onError of sub will not be called, right?

0
source share

All Articles