问题描述
我在下面有一个简单的代码
I have a simple code below
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毫秒,以确保链中的睡眠已开始并进行处理。
It purposely use Thread.sleep(1000)
, just to trigger the InterruptedException
. I purposely delay 100 milliseconds, so that ensure the sleep in the chain has started, and dispose it.
(注意,我不喜欢使用 Thread.sleep
。我只是将此代码编写为测试并了解为什么在这种情况下未调用 onError
,以及如何在RxJava链中无需使用try-catch的情况下优雅地防止崩溃)
(Note, I know using of Thread.sleep
is not preferred. I'm just writing this code to test and understand why onError
is not called on this scenario, and how to prevent the crash elegantly without need to use try-catch in the RxJava chain)
当时,触发它时,该错误不是引起 onError
的原因(即未达到日志
。但是相反,它引发以下错误并使应用程序崩溃。
At that time, when it is triggered, the error is not cause onError
(i.e. doesn't reach the Log
. But instead it throws the below error and crash the App.
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中的 onError
没有捕获 InterruptedException
? / p>
Why was't the InterruptedException
caught by the onError
in RxJava?
推荐答案
按照@akarnokd的说法,在's-different-in-2.0#error-handling,看起来RxJava之前已经被处置过抛出,因此后来抛出的错误得以解决。要解决该问题,只需注册
As per pointed by @akarnokd, in https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling, looks like the RxJava has been disposed before the throw, hence the error thrown later got through. To address the issue just need to register
RxJavaPlugins.setErrorHandler { e -> /*Do whatever we need with the error*/ }
这篇关于没有在RxJava onError回调上捕获InterruptedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!