onExceptionResumeNext() is a great way to restore the flow processing from some other Observable after an exception has occurred. It can be used as a mechanism to plug in the backup Observable if the original one fails. That's exactly what we will do in later sections to handle the cases when we cannot fetch the new stock quote data from the remote service.
However, let's get back to onExceptionResumeNext(), and let's take a look at example of how it can be used:
Observable.<String>error(new RuntimeException("Crash!"))
.onExceptionResumeNext(Observable.just("Second"))
.subscribe(item -> {
log("subscribe", item);
}, e -> log("subscribe", e));
This time, we won't see any exceptions in the logs and the second (Second) item will be processed normally. The following message will appear in the logs:
subscribe:main:Second
The item for this message was taken from the second Observable that was created with this:
Observable.just("Second")
So, when the exception occurred, it reached the onExceptionResumeNext() operator, and it resumed the sequence from the second Observable.
Also, in this case, we have used a special Observable that just emits an error and ceases its operation:
Observable.<String>error(new RuntimeException("Crash!"))
Basically, it's a shortcut to throw an exception in an Observable-like fashion.
This method will become extremely useful in any developer's toolbox when working with RxJava.