O'Reilly logo

Learning Reactive Programming with Java 8 by Nickolay Tsvetinov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Handling errors

When dealing with errors in RxJava, you should be aware that they terminate the Observable chain of actions. Much like with your normal procedural code, once you are in the catch block, you can't go back to the code that has thrown the exception. You can execute some backup logic though and use it instead of failing the program. The return*, retry*, and resume* operators do something similar.

The return and resume operators

The onErrorReturn operator can be used in order to prevent the Subscriber instance's onError from being called. Instead, it will emit one last item and complete. Here is an example:

Observable<String> numbers = Observable
  .just("1", "2", "three", "4", "5")
  .map(Integer::parseInt)
  .onErrorReturn(e -> -1); subscribePrint(numbers, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required