问题描述
有人可以,请解释为什么此代码会引发无法传递的异常以及如何解决该异常:
val completable = Completable.fromAction { Thread.sleep(10000) }.subscribeOn(Schedulers.io())
if (!completable.blockingAwait(5,TimeUnit.SECONDS)) {
System.out.println("timeout triggered")
} else {
System.out.println("all good")
}
运行此代码时,将导致以下异常:
08-22 14:01:57.575 31345-31345/com.myapp I/System.out: timeout triggered
08-22 14:01:57.593 31345-31399/com.myapp [CRASH] : The exception Could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has Nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.InterruptedException,Abort
Exception: The exception Could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has Nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.lang.InterruptedException`
解决方法
您没有订阅此完成项,因此您无法处理此异常。您必须设置一些回调来处理此类异常:
RxJavaPlugins.setErrorHandler { e ->
// do something
}
您可以详细了解here