简体   繁体   English

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

[英]Exception isn't caught in a handler message

The situation is quite complicated, but I'll try to explain it. 情况非常复杂,但我将尽力解释。 (The code is simplified from the real app) (代码从实际应用程序中简化了)

I have a handler on a custom thread with a looper. 我在带有弯针的自定义线程上有一个处理程序。 I send a message to it: 我向它发送一条消息:

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

The handler has a message callback: 处理程序具有消息回调:

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;
    }
};

I have a RxJava wrapper on sending this message. 我在发送此消息时有一个RxJava包装器。

If I just subscribe, everythig is fine. 如果我只订阅,everythig很好。 But if I do subscribe then unsubscribe the exception isn't caught and the app crashed. 但是,如果我确实订阅了,则取消订阅不会捕获到异常,并且应用程序崩溃了。 The RxJava wrapper doesn't do any work except sending the message in onSubscribe . 除了在onSubscribe发送消息外,RxJava包装器不会做任何工作。

Any suggesions, how is it possible? 任何建议,怎么可能?

The stack trace: 堆栈跟踪:

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)

I found a problem. 我发现了一个问题。 After updating RxJava to the latest version (2.1.2) stack trace became more obvious. 将RxJava更新到最新版本(2.1.2)后,堆栈跟踪变得更加明显。 And I realized that even if exception is caught I call onError on my observable with my exception that I actually store in the catch block (the code isn't shown here). 而且我意识到,即使捕获到异常,我也会在可观察对象上调用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(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是不是在try块中捕获的未经检查的异常在Java中检查了异常? - Isn't an unchecked exception that is caught in a try block a checked exception in Java? jdb打印捕获到异常消息 - jdb print caught exception message Matlab中的错误:捕获到“ std :: exception”异常消息是: - Error in Matlab: Caught “std::exception” Exception message is: 一旦被异常捕获,便从异常处理程序重定向到控制器的动作? - redirecting to the controller's action from exception handler once it caught by the exception? Java中的子类化异常:何时不是自定义消息“足够好”? - Subclassing Exception in Java: when isn't a custom message “good enough”? 在 Java 中,我们抛出的异常没有被默认处理程序捕获? 正确的 - In Java, An exception thrown by us is not caught by the default handler? Right 从@ExceptionHandler 抛出异常以被另一个处理程序捕获 - Throwing an exception from @ExceptionHandler to get caught by another handler 无法显示捕获的异常(newSingleThreadScheduledExecutor) - can't show caught exception (newSingleThreadScheduledExecutor) 没有发现异常吗? - No exception caught? Spring 引导 404:在 Controller 中未发现异常 - Spring Boot 404 : no handler found exception in Controller Advice isn't catched
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM