RxJava1 vs Rxjava2: Exception on onNext

When executing the following in RxJava1, an exception in onNext will be redirected to the same Subscriber onError:

    Observable.from(Arrays.asList("1", "22", "333", "4444")).subscribe(new Subscriber<String>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            Log.d("RxJava1", "onError: " + e.getCause());
        }

        @Override
        public void onNext(String s) {
            if (s.length() == 4) {
                Integer test = null;
                test.hashCode();
            }
            Log.d("RxJava1", s + " - " + s.length());
        }
    });

output:

D/RxJava1: 1 - 1
D/RxJava1: 22 - 2
D/RxJava1: 333 - 3
D/RxJava1: onError: null

When performing, as far as I know, the same in RxJava2, this behavior has changed and no longer returns to onError, but just crashes:

    Observable.fromIterable(Arrays.asList("1", "22", "333", "4444")).subscribeWith(new Observer<String>() {
        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(String s) {
            if (s.length() == 4) {
                Integer test = null;
                test.hashCode();
            }
            Log.d("RxJava2", s + " - " + s.length());
        }

        @Override
        public void onError(Throwable e) {
            Log.d("RxJava2", "onError: " + e.getCause());
        }

        @Override
        public void onComplete() {

        }
    });

Conclusion:

D/RxJava2: 1 - 1
D/RxJava2: 22 - 2
D/RxJava2: 333 - 3
D/AndroidRuntime: Shutting down VM

I would like to know which of the two versions does this “wrong”? Was it a bug in RxJava1 that was fixed? Is this a bug in RxJava2? Or was this not a conscious change in the first place, since I cannot find any details about it?

ps. I noticed that porting this to "SafeObserver" again redirects to onError

+4
source share
1 answer

, . 2.x Observer/Subscriber onXXX.

1-2-3 subscribe() 2.x, . , onNext, safeSubscribe().

+4

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


All Articles