Android Java try catch 失效问题及解决
作者:吃饱很舒服 发布时间:2023-06-17 17:07:33
参考:
How to catch an Exception from a thread
Is there a way to make Runnable's run() throw an exception?
Java捕获线程异常的几种方式
如果你在 异常抛出处 的 外层函数 中添加了 try catch 不生效的话, 就试试下面的办法吧.
解决办法
方法一
如果在 异常抛出处 或 外层调用函数中 使用了 Runnable run 函数, try catch 需要添在 run 函数里面, 如下:
new Thread(new Runnable() {
@Override
public void run() {
try {
throw new IllegalArgumentException("test exception");
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
如果使用的是第三方库, 无法捕获 Runnable run 函数中的异常时, 则可在 Runnable 之前添加如下代码解决(需注意: 此方法在 Android 中子线程可用, 主线程仍会 crash):
// 在调用第三方库前先执行下面代码
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
// 这里就可以捕获到第三方库的异常了
}
});
// 假如这里是一个第三方库抛出异常的地方
new Thread(new Runnable() {
@Override
public void run() {
// 子线程 -> 抛出异常
throw Exception("unknown exception");
}
}).start();
在 Android 中, 如果无法捕获 Runnable run 函数中的异常, 并且是在主线程调用, 就只能想办法避免 crash 了.
比如我是在调用 show 函数之前有网络请求, 网络请求成功后, 此页面已不在前台, 才会导致 crash; 可以在网络请求成功后, 判断此页面是否在前台展示, 再执行相关操作.
事情起因
新版上线后, 出现了这个 crash. 经排查, 发现 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)
根据 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 会调用到异常抛出的位置
showGuidePage();
...
}
});
}
发现在 show 函数中, 有关键代码 mParentView.post(runnable), 此时, 异常就是在 run 函数中调用的 showGuidePage 中抛出的, 并且这个异常在主线程中, 主线程就会停止掉, 就会 crash!
来源:https://blog.csdn.net/HongHuaZu/article/details/122111081
猜你喜欢
- Android Bitmap和Drawable的对比Bitmap - 称作位图,一般位图的文件格式后缀为bmp,当然编码器也有很多如RGB5
- [程序中使用的数据结构和符号说明]HitBrick类GreenBallThread控制小球路线xUp,yUp,bouncing定义变量存储1
- 目录一、二叉树的顺序存储1.堆的存储方式2.下标关系二、堆(heap)1.概念2.大/小 根堆2.1小根堆2.2大根堆3.建堆操作3.1向下
- 本文介绍了maven下载jar包改用阿里云maven库的方法,分享给大家,具体如下:修改maven安装路径中conf文件夹下的setting
- 1. 快速创建maven管理的SpringBoot项目1、访问 http://start.spring.io/2、 选择构建工具Maven
- 要说this和super就不得不说Java的封装和继承了,首先说封装,这是一种思想,算不上一种技术,核心思想就是将对象的同一行为和状态看成是
- 我是因为构建多渠道包的时候有这个需求,平常工作多个渠道包频繁的打包,总会忘记versioncode提高一下,从而打包出来的apk无法覆盖原先
- C#事件使用+= -=使用起来是很方便的,但是却不能整体清空所有事件。比如一个常见的操作,打开界面注册监听事件,关闭界面需要把所有的事件清空
- 1、注解方式,在controller层通过initBinder注解实现@InitBinderpublic void initBinder(H
- 前言 之前的文章有介绍ActivityGroup,不少人问嵌套使用的问题,同样的需求在Fragment中也存在,幸好在最新的An
- 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——MyBatis的基础知识。MybatisFirst中存在大
- 老签名多渠道打包原理前言由于Android7.0发布了新的签名机制,加强了签名的加固,导致在新的签名机制下无法通过美团式的方式再继续打多渠道
- 类加载器类加载器作用负责将.class文件(存储的物理文件)加载到内存中类加载器过程加载:通过全类名获取这个类准备用流传输,加载进内存,加载
- 1. 你知道线程安全问题吗?线程安全问题:一般指在多线程模式下,多个线程对同一个共享数据进行操作时,第一个线程还没来得及更新共享数据,从而导
- 导入mybatis jar包右键pom.xml模拟springboot底层实现类1.定义接口@Mapperpublic interface
- 在观察者模式中有2个要素:一个是被观察对象,另一个是观察者。但被观察对象的状态发生改变会通知观察者。举例:把订阅报纸的人看作是观察者,把报纸
- 这篇文章主要介绍了Spring Cloud Hystrix异常处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 这是android新推出的一个,让卡片带立体感的一个控件,就是一个卡牌,有点类似于布局那种的东西,里面可以添加控件内容先看看运行的效果图:1
- 导出Excel在很多项目中经常用到,本人介绍了C#实现GridView导出Excel实例代码,也全当给自己留下个学习笔记了。using Sy
- 我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在大图显示页面,中