DoOnError does not throw an exception

I am just starting with RxJava, but maybe something else has not clicked.

1.

Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1};
Observable.from(items)
        .map(this::invert)
        .subscribe(i -> Log.d(LOG_TAG, "Inverted: " + i), t -> Log.d(LOG_TAG, "Error: " + t.getMessage()));

2.

Integer[] items = {1, 2, 3, 0, 0, 4, 5, 6, 1};
Observable.from(items)
        .map(this::invert)
        .doOnError(t -> Log.d(LOG_TAG, "Error: " + t.getMessage()))
        .doOnNext(i -> Log.d(LOG_TAG, "Inverted: " + i))
        .subscribe();

Function invert:

int invert(int i) {
    return 1 / i;
}

The first is executed normally, and when an exception is thrown, it is executed onError. But, on the other hand, the second does not work, so the exception extends all the way to the calling method.

What is the difference between two blocks of code?

+4
source share
1 answer

Keep in mind that .doOnError()catches an exception, does something to it, and then throws it again. If you need other behavior, use one of the methods .onError*.

, # 1, # 2 , # 1, # 2, .

+5

Source: https://habr.com/ru/post/1666007/


All Articles