在 RxJava/RxKotlin 中,返回 Completable.error(Exception()) 和抛出有什么区别?

In RxJava/RxKotlin, what are the differences between returning a Completable.error(Exception()) and throwing?

以下情况有什么区别:

fun a(params: String) = Completable.fromAction {
        if (params.isEmpty()) {
            throw EmptyRequiredFieldException() 
        }
    }

VS

fun b(params: String) = if(params.isEmpty()) 
       Completable.error(EmptyRequiredFieldException()) 
else 
       Completable.complete()

特别是在 android 的上下文中,如果它很重要(即使我不认为它很重要) 谢谢!

根据documentation

If the Action throws an exception, the respective Throwable is delivered to the downstream via CompletableObserver.onError(Throwable), except when the downstream has disposed this Completable source. In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.

所以你描述的两种方式都是类似的(除非下游已经处理)。请注意,第一种方法(手动抛出异常)允许在运行时修改 Completable 的行为。第二个 - 静态定义为 return 特定类型的 Completable 并且无法修改它。

根据自己的需要选择什么。