【发布时间】:2018-10-26 07:40:27
【问题描述】:
假设我需要用 Flowable 包装 BroadcastReceiver:
Flowable
.create<Boolean>({ emitter ->
val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
throw RuntimeException("Test exception")
}
}
application.registerReceiver(broadcastReceiver, IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION))
}, BackpressureStrategy.MISSING)
.onErrorReturn { false }
然后我需要在一个地方捕获 Flowable 中抛出的任何异常。
我认为onErrorReturn 应该能够在广播接收器中捕捉到 throw RuntimeException("Test exception"),但它没有捕捉到该异常和应用程序崩溃。
当然,我可以使用try/catch 在 BroadcastReceiver 中封装任何内容。但实际上,我那里有很多源代码,因此添加try/catch 会使源代码变得非常混乱。
有什么方法可以在一个地方捕获 Flowable 内任何一行中抛出的所有异常?
【问题讨论】:
标签: exception kotlin exception-handling broadcastreceiver rx-java2