Reputation: 10273
I am trying to perform some IO operation in the Schedulers IO thread, and if an exception is thrown, I want to show some Toast to the user (in the mainThread, of course).
For this I use the following code:
try {
Observable.interval(0, 15, TimeUnit.SECONDS)
.observeOn(Schedulers.io())
.doOnNext(event -> failingNetworkOperation();)
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable ->
Toast.makeText(MainActivity.this, "Error message", LENGTH_SHORT).show())
.subscribe();
} catch(Throwable t) {
//THIS CODE IS NEVER REACHED
//BUT THE APPLICATION DIES.
t.printStackTrace();
}
When an exception is thrown on method "failingInputOutputOperation", the Throwable IS catched in the "doOnError" consumer, but the application dies.
I sorrounded the whole snippet in a try/catch bloc, but the exception is never caught there.
Why is the application dying and how can I correctly manage the error with RxJava? Thank you very much!
Upvotes: 0
Views: 177
Reputation: 69997
Your app dies because you don't have an error handler in subscribe
. Put the callback of the doOnError
there:
Observable.interval(0, 15, TimeUnit.SECONDS, Schedulers.io())
.doOnNext(event -> failingNetworkOperation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(items -> { },
throwable ->
Toast.makeText(MainActivity.this, "Error message", LENGTH_SHORT).show()
);
Upvotes: 4