[英]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.