【Android】Analysis of OnErrorReturn in RxJava

96 篇文章 2 订阅
About onErrorReturn

onErrorReturn is an rxjava operator that enable emit a placeholder value instead of emit an error

Now let’s try to find what will happen after using this operator

Trial Code

Take a look at code snippet below

import io.reactivex.rxjava3.core.Observable

fun main() {
    Observable.just(Unit)
        .doOnNext { println("next 1") }
        .doOnError { println("error 1") }
        .map<String> { throw RuntimeException("error") }
        .doOnNext { println("next 2") }
        .doOnError { println("error 2") }
        .onErrorReturn { "error placeholder 3" }
        .doOnNext { println("next 3") }
        .doOnError { println("error 3") }
        .doOnComplete { println("complete 4") }
        .doFinally { println("finally 4") }
        .subscribe()
}
Console Outputs

This is the result of execution above

1
4
5
7
9
Summary

We can take a conclusion of those

  • doOnError before onErrorReturn will catch error
  • doOnError after onErrorReturn will not catch error
  • doOnNext will receive the placeholder value
  • doOnComplete and doFinally will be called, means the whole process is regard as successful
  • onErrorReturn can only replace error occurs above, which is not tested here
Bless

Good Work, Good Study, Good Progress, and Good Mood !

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值