简体   繁体   English

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

[英]InterruptedException not caught on RxJava onError callback?

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)

It purposely use Thread.sleep(1000) , just to trigger the InterruptedException . 它故意使用Thread.sleep(1000) ,只是为了触发InterruptedException I purposely delay 100 milliseconds, so that ensure the sleep in the chain has started, and dispose it. 我故意延迟100毫秒,以确保链中的睡眠已开始并进行处理。

(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) (请注意,我知道不推荐使用Thread.sleep 。我只是编写此代码来测试并了解为什么在这种情况下不调用onError ,以及如何在不使用try-catch的情况下优雅地防止崩溃。 RxJava链)

At that time, when it is triggered, the error is not cause onError (ie doesn't reach the Log . But instead it throws the below error and crash the App. 当时,触发它时,该错误不是导致onError (即未到达Log 。)而是引发以下错误并使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) 

Why was't the InterruptedException caught by the onError in RxJava? 为什么was't的InterruptedException被抓onError在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. 正如@akarnokd指出的那样,在https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling中 ,看起来RxJava已经在抛出之前被丢弃了,因此出现了错误抛出以后通过。 To address the issue just need to register 要解决此问题,只需注册

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM