io.reactivex.exceptions.UndeliverableException

io.reactivex.exceptions.UndeliverableException:java.net.UnknownHostException: Unable to resolve host “xx.xx.com”: No address associated with hostname
io.reactivex.plugins.RxJavaPlugins.void onError(java.lang.Throwable)(SourceFile:349)
在bugly中捕获到此问题,项目中用到的Rxjava操作符是Flowable一路追踪其subscribe方法发现了RxJavaPlugins.onError的身影:

   @BackpressureSupport(BackpressureKind.SPECIAL)
    @SchedulerSupport(SchedulerSupport.NONE)
    @Override
    public final void subscribe(Subscribersuper T> s) {
        ObjectHelper.requireNonNull(s, "s is null");
        try {
            s = RxJavaPlugins.onSubscribe(this, s);

            ObjectHelper.requireNonNull(s, "Plugin returned null Subscriber");

            subscribeActual(s);
        } catch (NullPointerException e) { // NOPMD
            throw e;
        } catch (Throwable e) {
            Exceptions.throwIfFatal(e);
            // can't call onError because no way to know if a Subscription has been set or not
            // can't call onSubscribe because the call might have set a Subscription already

            **RxJavaPlugins.onError(e);//这里调用了**

            NullPointerException npe = new NullPointerException("Actually not, but can't throw other exceptions due to RS");
            npe.initCause(e);
            throw npe;
        }
    }

错误信息中出现的这个onError语句,然后点击查看其实现方法

public static void onError(@NonNull Throwable error) {
        Consumersuper Throwable> f = errorHandler;

        if (error == null) {
            error = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
        } else {
            if (!isBug(error)) {
                error = new UndeliverableException(error);
            }
        }

        if (f != null) {//如果f不为null则就会调用f的accept方法
            try {
                f.accept(error);
                return;
            } catch (Throwable e) {
                // Exceptions.throwIfFatal(e); TODO decide
                e.printStackTrace(); // NOPMD
                uncaught(e);
            }
        }

        error.printStackTrace(); // NOPMD
        uncaught(error);
    }

–uncaught将UndeliverableException抛出?
如果f不为null则就会调用f的accept方法,f为errorHandler,然后就在RxJavaPlugins中搜索这个变量,发现其赋值方法:

/**
 * Sets the specific hook function.
 * @param handler the hook function to set, null allowed
 */
public static void setErrorHandler(@Nullable Consumersuper Throwable> handler) {
    if (lockdown) {
        throw new IllegalStateException("Plugins can't be changed anymore");
    }
    errorHandler = handler;
}

这样我们就可以通过设置errorHandler来捕获UndeliverableException异常。

private void setRxJavaErrorHandler() {
if(RxJavaPlugins.getErrorHandler()||RxJavaPlugins.lockdown) return;
   RxJavaPlugins.setErrorHandler(e -> {
    if (e instanceof UndeliverableException) {
        e = e.getCause();
    }
    if ((e instanceof IOException) || (e instanceof SocketException)) {
        // fine, irrelevant network problem or API that throws on cancellation
        return;
    }
    if (e instanceof InterruptedException) {
        // fine, some blocking code was interrupted by a dispose call
        return;
    }
    if ((e instanceof NullPointerException) || (e instanceof IllegalArgumentException)) {
        // that's likely a bug in the application
        Thread.currentThread().getUncaughtExceptionHandler()
            .handleException(Thread.currentThread(), e);
        return;
    }
    if (e instanceof IllegalStateException) {
        // that's a bug in RxJava or in a custom operator
        Thread.currentThread().getUncaughtExceptionHandler()
            .handleException(Thread.currentThread(), e);
        return;
    }
    Log.warning("Undeliverable exception received, not sure what to do", e);
});
}

参考:
http://blog.csdn.net/sr_code_plus/article/details/77189478
https://github.com/ReactiveX/RxJava/wiki/What’s-different-in-2.0#error-handling

你可能感兴趣的:(android,安卓错误集锦)