处理程序消息中未捕获异常

时间:2017-08-02 18:21:34

标签: java android multithreading rx-java2

情况相当复杂,但我会试着解释一下。 (代码从真实应用程序简化)

我在带有looper的自定义线程上有一个处理程序。 我发了一条消息:

...
handler.obtainMessage(...)
    .sendToTarget();
...

处理程序有一个消息回调:

private final Handler.Callback callback = new Handler.Callback() {
    public boolean handleMessage(Message msg) {
            try {
                //The hard work is here
                SystemClock.sleep(5000)
                throw new CustomException();
            } catch (CustomException e) {

            }
            //...
            //Sending the result back to the UI thread here.
            //...
        }
        return true;
    }
};

我发送此消息时有一个RxJava包装器。

如果我只是订阅,那么每个人都很好。 但是,如果我订阅然后取消订阅该异常未被捕获且应用程序崩溃。 除了在onSubscribe中发送消息外,RxJava包装器不做任何工作。

任何建议,怎么可能?

堆栈跟踪:

08-01 10:02:58.422 13655-13655/com.mycompany.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.mycompany.myapp, PID: 13655
                                                                         com.mycompany.myapp.util.CustomException
                                                                             com.mycompany.myapp.util.LooperThread$1.handleMessage(LooperThread.java:33)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                             at android.os.Looper.loop(Looper.java:173)
                                                                             at com.mycompany.myapp.util.neulion.LooperThread.run(LooperThread.java:57)

1 个答案:

答案 0 :(得分:0)

我发现了一个问题。将RxJava更新到最新版本(2.1.2)后,堆栈跟踪变得更加明显。我意识到即使异常被捕获,我也会在我的observable上调用onError,除了我实际存储在catch块中(此处未显示代码)。

08-03 10:53:25.811 6822-6822/com.mycompany.myapp E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.mycompany.myapp, PID: 6822
   io.reactivex.exceptions.UndeliverableException: java.lang.Exception: My exception
       at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:349)
       at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onError(ObservableCreate.java:74)
       at com.mycompany.myapp.ui.screen.video.RxNeulion$1$1.onFailure(RxNeulion.java:29)
       at com.mycompany.myapp.util.neulion.Neulion$Callback.run(Neulion.java:59)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:7303)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Caused by: java.lang.Exception: My exception
       at com.mycompany.myapp.util.neulion.MyHandlerCb.handleMessage(MyHandlerCb.java:27)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:148)
       at com.mycompany.myapp.util.neulion.LooperThread.run(