[英]In RxJava/RxKotlin, what are the differences between returning a Completable.error(Exception()) and throwing?
What are the differences in the following cases: 在以下情况下有什么区别:
fun a(params: String) = Completable.fromAction {
if (params.isEmpty()) {
throw EmptyRequiredFieldException()
}
}
VS VS
fun b(params: String) = if(params.isEmpty())
Completable.error(EmptyRequiredFieldException())
else
Completable.complete()
Specifically in the context of android, if it matters (even though I don't think it does) Thanks! 特别是在android的上下文中,如果它很重要(即使我认为不重要),谢谢!
According to 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.
如果操作引发异常,则除非下游已处置此Completable源,否则各自的Throwable将通过CompletableObserver.onError(Throwable)传递到下游。 In this latter case, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) as an UndeliverableException.
在后一种情况下,Throwable通过RxJavaPlugins.onError(Throwable)作为UndeliverableException传递到全局错误处理程序。
So both of two ways you described are similar (except when the downstream has disposed). 因此,您所描述的两种方式都是相似的(下游已废弃时除外)。 Note, that first approach (with manually throwing exception) allow to modify behavior of
Completable
at runtime. 请注意,第一种方法(具有手动引发的异常)允许在运行时修改
Completable
行为。 And second one - statically defined as you return particular type of Completable
and can't modify it. 第二个-静态定义为您返回特定类型的
Completable
且无法对其进行修改。
What to choose depends on your needs. 选择什么取决于您的需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.