error-handling - 错误没有从 rxjava zip 函数中的 observables 冒泡
问题描述
我试图了解 rxjava 中的错误处理。我想如果我在一个 zip() 函数中组合一个 observables 流,那么 zip 中的 observables 发出的错误会破坏序列并冒泡到订阅者 onError 函数。然而,唯一捕获的错误是 BiFunction 中发出的错误。链上发出的错误会导致系统崩溃。当我将 onErrorReturn 添加到可观察对象并返回后备值时,系统仍然崩溃。所以对我来说,这并不像我预期的那样工作。我错过了什么?
private fun getOneThing (): Single<String> {
println("getOneThing")
if (isOneBadCondition) {
throw Exception() //causes crash
} else {
return Single.just("a string thing")
}
}
private fun getAnotherThing(): Single<Boolean> {
println("getAnotherThing")
if (isAnotherBadCondition) {
throw Exception() //causes crash
} else {
return Single.just(true)
}
}
private fun createSomethingElse (): Int {
println("createAnother")
if (isBadCondition) {
throw Exception() //is handled onError
} else {
return 2
}
}
fun errorHandlingTest() {
Single.zip(
getOneThing(), //if I add onErrorReturn here it is not called after error
getAnotherThing(), //if I add onErrorReturn here it is not called after error
BiFunction<String, Boolean, Int> { t1, t2 ->
createSomethingElse()
}
).subscribeBy(
onSuccess ={ println(it) },
onError={ it.printStackTrace() }) //only error thrown by createSomethingElse() are caught here
}
解决方案
推荐阅读
- javascript - 我需要找到总工资,即佣金和工资的总和(JS)
- next.js - 图片未加载 NextJS 图片 403 错误
- asp.net-mvc - 允许在 Visual Studio 中对方法参数进行 ctrl+click 导航
- android - 如何单独/单独对齐行内的子可组合项?
- caching - “useProtocolCachePolicy”没有检索我的缓存
- python - 仅显示“重新排序”项目
- php - 从数据库下载的数据比较
- uninstallation - 由于来自另一个软件包的冲突,dpkg 安装失败
- android - Google 发布前报告中的应用程序崩溃报告致命例外:UserFacing7
- reactjs - 如何使用钩子将类组件转换为功能组件