O'Reilly logo

Reactive Android Programming by Tadas Subonis

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

Using subscribe()

First of all, the most common way to handle errors and exceptions is to use the .subscribe() method. The .subscribe() method takes an additional argument that is used to process the exceptions that the Observable or operators have thrown.

Let's take an example. Consider this piece of code:

Observable.just("One")        .doOnNext(i -> {            throw new RuntimeException();        })        .subscribe(item -> {            log("subscribe", item);        });

As it is now, it will kill the entire application because there is an exception thrown in the .doOnNext() operator. This can be easily mitigated using the second argument of the .subscribe() method:

Observable.just("One")        .doOnNext(i -> {            throw new RuntimeException();        })        .subscribe(item -> {            log("subscribe", item); ...

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