Android?Java?try?catch?失效問題及解決
參考:
- How to catch an Exception from a thread
- Is there a way to make Runnable's run() throw an exception?
- Java捕獲線程異常的幾種方式
如果你在 異常拋出處 的 外層函數(shù) 中添加了 try catch 不生效的話, 就試試下面的辦法吧.
解決辦法
方法一
如果在 異常拋出處 或 外層調(diào)用函數(shù)中 使用了 Runnable run 函數(shù), try catch 需要添在 run 函數(shù)里面, 如下:
new Thread(new Runnable() {
@Override
public void run() {
try {
throw new IllegalArgumentException("test exception");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();如果使用的是第三方庫, 無法捕獲 Runnable run 函數(shù)中的異常時(shí), 則可在 Runnable 之前添加如下代碼解決(需注意: 此方法在 Android 中子線程可用, 主線程仍會(huì) crash):
// 在調(diào)用第三方庫前先執(zhí)行下面代碼
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// 這里就可以捕獲到第三方庫的異常了
}
});
// 假如這里是一個(gè)第三方庫拋出異常的地方
new Thread(new Runnable() {
@Override
public void run() {
// 子線程 -> 拋出異常
throw Exception("unknown exception");
}
}).start();在 Android 中, 如果無法捕獲 Runnable run 函數(shù)中的異常, 并且是在主線程調(diào)用, 就只能想辦法避免 crash 了.
比如我是在調(diào)用 show 函數(shù)之前有網(wǎng)絡(luò)請求, 網(wǎng)絡(luò)請求成功后, 此頁面已不在前臺(tái), 才會(huì)導(dǎo)致 crash; 可以在網(wǎng)絡(luò)請求成功后, 判斷此頁面是否在前臺(tái)展示, 再執(zhí)行相關(guān)操作.
事情起因
新版上線后, 出現(xiàn)了這個(gè) crash. 經(jīng)排查, 發(fā)現(xiàn) crash 是從第三方庫中拋出的, 位置如下:
2021-12-23 17:39:57.408 3535-3535/com.podbean.app.podcast E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.podbean.app.podcast, PID: 3535
java.lang.IllegalArgumentException: the view is not showing in the window!
at com.app.hubert.guide.util.ViewUtils.getLocationInView(ViewUtils.java:47)
at com.app.hubert.guide.model.HighlightView.fetchLocation(HighlightView.java:77)
at com.app.hubert.guide.model.HighlightView.getRectF(HighlightView.java:67)
at com.app.hubert.guide.model.RelativeGuide.getMarginInfo(RelativeGuide.java:90)
at com.app.hubert.guide.model.RelativeGuide.getGuideLayout(RelativeGuide.java:76)
at com.app.hubert.guide.core.GuideLayout.addCustomToLayout(GuideLayout.java:227)
at com.app.hubert.guide.core.GuideLayout.onAttachedToWindow(GuideLayout.java:185)
at android.view.View.dispatchAttachedToWindow(View.java:20479)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3489)
at android.view.ViewGroup.addViewInner(ViewGroup.java:5278)
at android.view.ViewGroup.addView(ViewGroup.java:5064)
at android.view.ViewGroup.addView(ViewGroup.java:5036)
at com.app.hubert.guide.core.Controller.showGuidePage(Controller.java:175)
at com.app.hubert.guide.core.Controller.access$200(Controller.java:39)
at com.app.hubert.guide.core.Controller$1.run(Controller.java:118)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7664)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
根據(jù) log 信息, 最終我找到了這里
// ViewUitls.java
public static Rect getLocationInView(View parent, View child) {
...
if (tmp == null) {
// 異常拋出位置
throw new IllegalArgumentException("the view is not showing in the window!");
}
...
}
// Controller.java
public void show() {
...
// 使用 Runnable run 位置
mParentView.post(new Runnable() {
@Override
public void run() {
...
// showGuidePage 會(huì)調(diào)用到異常拋出的位置
showGuidePage();
...
}
});
}發(fā)現(xiàn)在 show 函數(shù)中, 有關(guān)鍵代碼 mParentView.post(runnable), 此時(shí), 異常就是在 run 函數(shù)中調(diào)用的 showGuidePage 中拋出的, 并且這個(gè)異常在主線程中, 主線程就會(huì)停止掉, 就會(huì) crash!
總結(jié)
起先, 我是自己 throw Exception, 但 try catch 都是正常生效的, 始終無法復(fù)現(xiàn)線上的 crash. 后來靈光一閃, 想到在子線程中拋出異常會(huì)怎樣呢? 經(jīng)過嘗試, 的確 catch 不到 子線程的 Exception, 具體原因不了解.... 或許我們可以把結(jié)果看成一個(gè)定義. 然后就在網(wǎng)上查了相關(guān)問題。
結(jié)論如下:
在 Java 中, 線程中的異常是不能拋出到調(diào)用該線程的外部方法中捕獲的.(我覺得這句話可改為, 在 Android 的 Runnable run 函數(shù)中的異常是不能拋出到外部方法中捕獲的. 因?yàn)樵?Android 的主線程中使用 Runnable run 函數(shù), 不在 run 函數(shù)中 try catch 的話, 仍會(huì) crash!)
因?yàn)榫€程是獨(dú)立執(zhí)行的代碼片斷, 線程的問題應(yīng)該由線程自己來解決, 而不要委托到外部. 基于這樣的設(shè)計(jì)理念, 在 Java 中, 線程方法的異常都應(yīng)該在線程代碼邊界之內(nèi)(run 方法內(nèi))進(jìn)行 try catch 并處理掉. 換句話說, 我們不能捕獲從線程中逃逸的異常.
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android進(jìn)階篇-自定義圖片伸縮控件具體實(shí)例
這篇文章介紹了Android自定義圖片伸縮控件具體實(shí)例,有需要的朋友可以參考一下2013-11-11
Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽
這篇文章主要為大家介紹了Android開發(fā)手冊Chip監(jiān)聽及ChipGroup監(jiān)聽,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
這篇文章將借助開源庫?MLKit?實(shí)現(xiàn)條形碼掃描,對于商品條形碼也可以很好地識(shí)別成功,該庫的使用內(nèi)容非常豐富,除了條碼識(shí)別,還有文字識(shí)別、圖像標(biāo)記、人臉檢測等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下2023-08-08
Android開發(fā)自定義雙向SeekBar拖動(dòng)條控件
這篇文章主要為大家介紹了Android開發(fā)自定義雙向SeekBar拖動(dòng)條控件使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06

