V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
nnegier
V2EX  ›  Android

怎样才可以 try-catch 方式捕获到 RxJava 的异常

  •  
  •   nnegier · Apr 22, 2023 · 12245 views
    This topic created in 1107 days ago, the information mentioned may be changed or developed.

    我知道 RxJava 链式有专门的回调处理异常,但是我是希望它抛出来,这样我后续的一系列的同质代码就不用再一一执行了。

    我用的是 Observable ,代码如下:

    NginxService service = getRetrofit().create(NginxService.class);
    service.updateClassification(SPUtils.getUserID(), classification)
            .blockingSubscribe(result -> {
                Logger.e(result.toString());
                if (result.isSuccessful()) {
                    classificationJobDao.delete(localJobID);
                }
            });
    

    上面的代码在外面加 try-catch 是捕获不到的,虽然这里用的是 blockingSubscribe 相当于是同步方式,但是不行,所以我也不知道为什么捕获不到,搜索未果,特来询问

    2 replies    2023-04-22 12:32:41 +08:00
    Jonney
        1
    Jonney  
       Apr 22, 2023 via iPhone   ❤️ 1
    将 blockingSubscribe 更改为 blockingGet
    Edward4074
        2
    Edward4074  
       Apr 22, 2023
    多年以前用 RxJava1 的时候,是通过自定义一个 Observer 封装 onError 逻辑处理的,后面的版本没用过,应该也可以类似的方式实现
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   766 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 21:13 · PVG 05:13 · LAX 14:13 · JFK 17:13
    ♥ Do have faith in what you're doing.