Sergey Aldoukhov
Sergey Aldoukhov

Reputation: 22744

Can RxJavaErrorHandler prevent an error from crashing the Android app?

I'm trying to suppress an error with RX plugin, but the app is still crashing. Am I doing anything wrong or plugin error handler is just for reporting and cannot prevent the crash?

    public void testClick(View view) {
    RxJavaPlugins.getInstance().registerErrorHandler(new RxJavaErrorHandler() {
        @Override
        public void handleError(Throwable e) {
            e.printStackTrace();
        }
    });

    final PublishSubject<Integer> hot = PublishSubject.create();
    hot
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<Integer>() {
                @Override
                public void call(Integer value) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    logger.info("Result");
                }
            });
    Observable.range(0, 100).subscribe(hot);
}

Upvotes: 1

Views: 801

Answers (1)

krp
krp

Reputation: 2247

If you look at _onError method in SafeSubscriber class you'll find :

try {
    RxJavaPlugins.getInstance().getErrorHandler().handleError(e);
} catch (Throwable pluginException) {
    handlePluginException(pluginException);
}
try {
    actual.onError(e);
} catch {
    ...
}

You can see that RxJavaPlugins ErrorHandler doesn't affect further error processing and it should be used to log/report errors

Upvotes: 4

Related Questions