Android?Java?try?catch失效问题如何解决
这篇文章主要介绍了AndroidJavatrycatch失效问题如何解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇AndroidJavatrycatch失效问题如何解决文章都会有所收获,下面我们一起来看看吧。
解决办法
方法一
如果在 异常抛出处 或 外层调用函数中 使用了 Runnablerun 函数, try catch 需要添在 run 函数里面, 如下:
newThread(newRunnable(){@Overridepublicvoidrun(){try{thrownewIllegalArgumentException("testexception");}catch(Exceptione){e.printStackTrace();}}}).start();
如果使用的是第三方库, 无法捕获 Runnable run 函数中的异常时, 则可在 Runnable 之前添加如下代码解决(需注意: 此方法在 Android 中子线程可用, 主线程仍会 crash):
//在调用第三方库前先执行下面代码Thread.setDefaultUncaughtExceptionHandler(newThread.UncaughtExceptionHandler(){@OverridepublicvoiduncaughtException(Threadt,Throwablee){//这里就可以捕获到第三方库的异常了}});//假如这里是一个第三方库抛出异常的地方newThread(newRunnable(){@Overridepublicvoidrun(){//子线程->抛出异常throwException("unknownexception");}}).start();
在 Android 中, 如果无法捕获Runnable run 函数中的异常, 并且是在主线程调用, 就只能想办法避免 crash 了.
比如我是在调用 show 函数之前有网络请求, 网络请求成功后, 此页面已不在前台, 才会导致crash; 可以在网络请求成功后, 判断此页面是否在前台展示, 再执行相关操作.
事情起因
新版上线后, 出现了这个 crash. 经排查, 发现 crash 是从第三方库中抛出的, 位置如下:
2023-12-23 17:39:57.408 3535-3535.podbean.app.podcast E/AndroidRuntime: FATAL EXCEPTION: main
Process:.podbean.app.podcast, PID: 3535
java.lang.IllegalArgumentException: the view is not showing in the window!
at.app.hubert.guide.util.ViewUtils.getLocationInView(ViewUtils.java:47)
at.app.hubert.guide.model.HighlightView.fetchLocation(HighlightView.java:77)
at.app.hubert.guide.model.HighlightView.getRectF(HighlightView.java:67)
at.app.hubert.guide.model.RelativeGuide.getMarginInfo(RelativeGuide.java:90)
at.app.hubert.guide.model.RelativeGuide.getGuideLayout(RelativeGuide.java:76)
at.app.hubert.guide.core.GuideLayout.addCustomToLayout(GuideLayout.java:227)
at.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.app.hubert.guide.core.Controller.showGuidePage(Controller.java:175)
at.app.hubert.guide.core.Controller.access$200(Controller.java:39)
at.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.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
根据 log 信息, 最终我找到了这里
//ViewUitls.javapublicstaticRectgetLocationInView(Viewparent,Viewchild){...if(tmp==null){//异常抛出位置thrownewIllegalArgumentException("theviewisnotshowinginthewindow!");}...}//Controller.javapublicvoidshow(){...//使用Runnablerun位置mParentView.post(newRunnable(){@Overridepublicvoidrun(){...//showGuidePage会调用到异常抛出的位置showGuidePage();...}});}
发现在 show 函数中, 有关键代码 mParentView.post(runnable), 此时, 异常就是在 run 函数中调用的showGuidePage 中抛出的, 并且这个异常在主线程中, 主线程就会停止掉, 就会crash!
关于“AndroidJavatrycatch失效问题如何解决”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“AndroidJavatrycatch失效问题如何解决”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注主机评测网行业资讯频道。
上一篇:vue3+vite中如何使用import.meta.glob