Kotlin 中的 PublishSubject onError 函数

PublishSubject onError function in Kotlin

因此,当我尝试订阅 PublishSubject 时,我的 Android 应用程序 (Kotlin) 出现错误。

错误解释非常简单明了,但是,我尝试实现这个 onError 函数失败了,我不确定如何以上帝的方式做到这一点。

错误在这里

The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | com.androidnetworking.error.ANError

这里是 PublishSubject:

var positionSubject = PublishSubject.create<Location>()

这里当我订阅时(在订阅代码中给出错误):

compositeDisposable.add(
                    positionSubject.subscribe {
                    // do some actions here that causes Exception
                    }
 )

这里我尝试以一种“不错”的方式修复它(没有用,仍然在订阅时崩溃):

compositeDisposable.add(
                        positionSubject
                            .onErrorReturn { t -> 
                                 Log.d("debug", "EXCEPTION OCCURRED")
                                 Location("")}
                            .subscribe {
                        // do some actions here that causes Exception
                        }
     )

这里是我最终修复它而不是崩溃的方法:

compositeDisposable.add(
                        positionSubject.subscribe {
                             try{
                                  // do some actions here that causes Exception
                             }catch(e:Exception){
                              Log.d("debug", "EXCEPTION OCCURRED $e")
                             }
                        
                        }
     )

我想知道如何以一种比在订阅中使用 try/catch 块更简洁的方式做到这一点,如果可能的话。

以下代码是 kotlin 订阅 PublishSubject

的方式
var positionSubject = PublishSubject.create<Location>()
positionSubject.subscribe({ location ->
    
}, { error ->
    
})

这应该没问题。