Search⌘ K
AI Features

Error-handling Operators

Explore how to handle errors effectively in RxJava streams by using operators that prevent stream termination. Understand when to use onErrorReturnItem, onErrorReturn, and onErrorResumeNext to keep your reactive flows resilient and adaptable to failures, such as fetching images from cache or network.

Delivering errors

As we’ve seen, when an error is encountered in a stream, RxJava delivers the error to the Observer via .onError(). Given that .onError() is terminal, no further processing will occur. However, this can be problematic, as programmer Dan Lew explains in his blog post:

Error Handling in RxJava

"I want to clear up something that many RxJava beginners get wrong: onError is an extreme event that should be reserved for times when sequences cannot continue. It means that there was a problem in processing the current item such that no future processing of any items can occur.

It’s the Rx version of try-catch: it skips all code between the exception and onError. Sometimes you want this behavior: suppose you’re downloading data then saving it to the disk. If the ...