没有在RxJava onError回调上捕获InterruptedException?

时间:2019-08-25 12:52:43

标签: java exception kotlin rx-java2 thread-sleep

我在下面有一个简单的代码

    compositeDisposable.add(Observable.create<Int> { Thread.sleep(1000) }
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe({}, {Log.d("Track", it.localizedMessage)}, {}))
    Handler().postDelayed({compositeDisposable.clear()}, 100)

它故意使用Thread.sleep(1000),仅用于触发InterruptedException。我故意延迟100毫秒,以确保链中的睡眠已开始并进行处理。

(注意,我知道不推荐使用Thread.sleep。我只是写这段代码来测试并了解为什么在这种情况下未调用onError,以及如何优雅地防止崩溃无需在RxJava链中使用try-catch)

那时,触发它时,错误不是引起onError(即未到达Log。)而是引发以下错误并使应用程序崩溃。

io.reactivex.exceptions.UndeliverableException: java.lang.InterruptedException
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:366)
    at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onError(ObservableCreate.java:74)
    at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:43)
    at io.reactivex.Observable.subscribe(Observable.java:11194)
    at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:463)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: java.lang.InterruptedException
    at java.lang.Thread.sleep(Native Method)
    at java.lang.Thread.sleep(Thread.java:373)
    at java.lang.Thread.sleep(Thread.java:314)
    at com.elyeproj.porterduff.AnimateDrawPorterDuffView$startAnimate$1.subscribe(AnimateDrawPorterDuffView.kt:45)
    at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
    at io.reactivex.Observable.subscribe(Observable.java:11194) 
    at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96) 
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:463) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66) 
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:764) 

为什么RxJava中InterruptedException没有被onError捕获?

1 个答案:

答案 0 :(得分:0)

根据@akarnokd的指出,在https://github.com/ReactiveX/RxJava/wiki/What的2.0差错处理中,RxJava似乎在抛出之前已被处理掉,因此后来抛出的错误得以解决。要解决该问题,只需注册

 RxJavaPlugins.setErrorHandler { e -> /*Do whatever we need with the error*/ }