UncaughtExceptionHandler 小结

点击按钮触发空指针异常后,程序没有立即退出,而是先打印了空指针的异常:

D/com.lee.clientapplication.ClientApplication: thread=2,throwable=null

但是我们点击屏幕并没有任何的反馈,最终还是抛出了应用出错的提醒。

2.2 主进程 + 子线程

class ClientActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById(R.id.bt).setOnClickListener {
threadException()
}
}

private fun threadException() {
thread {
val exception: String? = null
exception!!.length
}
}

}

现在我们在子线程当中触发空指针的异常,在这种情况下,仍然可以看到全局捕获的打印,但是用户仍然可以和页面进行交互:

2021-03-08 13:16:55.015 9516-9673/com.lee.clientapplication D/com.lee.clientapplication.ClientApplication: thread=8879,throwable=null

而假如我们去掉了 Application 中对于异常的全局捕获,那么程序还是会崩溃的。

三、UncaughtExceptionHandler 由谁触发的

我们可以看下官方文档对于这个函数的解释:

Interface for handlers invoked when a Thread abruptly terminates due to an uncaught exception.
When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() and will invoke the handler’s uncaughtException method, passing the thread and the exception as arguments. If a thread has not had its UncaughtExceptionHandler explicitly set, then its ThreadGroup object acts as its UncaughtExceptionHandler. If the ThreadGroup object has no special requirements for dealing with the exception, it can forward the invocation to the default uncaught exception handler.

当一个线程即将因不受捕获的异常即将终止时,JVM 尝试会使用 Thread.getUncaughtExceptionHandler() 方法获得该线程的 UncaughtExceptionHandler 对象并调用其 uncaughtException 方法,其参数为该线程和异常信息。如果没有设置,那么 ThreadGroup 将会作为默认的 UncaughtExceptionHandler,如果 ThreadGroup 也没有处理,那么会采用 default uncaught exception handler。

由此可见 UncaughtExceptionHandler 是由虚拟机通过 dispatchUncaughtException 触发的,调用链为:

  • UncaughtExceptionHandler:线程的成员变量,如果没有设置 UncaughtExceptionHandler,那么会调用 group 成员处理。

public class Thread implements Runnable {

public UncaughtExceptionHandler getUncaughtExceptionHandler() {
return uncaughtExceptionHandler != null ?
uncaughtExceptionHandler : group;
}

public final void dispatchUncaughtException(Throwable e) {
getUncaughtExceptionHandler().uncaughtException(this, e);
}
}

  • ThreadGroup:group 也是实现了 UncaughtExceptionHandler 接口,内部逻辑是先委托其 parent 处理,直到 parent 为空时,最终 parent 为空时才会走到调用 sDefaultUncaughtExceptionHandler。

public class ThreadGroup implements Thread.UncaughtExceptionHandler {

public void uncaughtException(Thread t, Throwable e) {
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread “”

  • t.getName() + “” ");
    e.printStackTrace(System.err);
    }
    }
    }
    }
  • sDefaultUncaughtExceptionHandler:线程的静态成员变量,用于处理该进程中的所有线程,也就是我们在上一部分所举的例子。

这里解释一下:

  • 对于子线程来说,其 ThreadGroup 为主线程 [name=main, maxpri=10]
  • 主线程的 parent 也是一个 ThreadGroup [name=system, maxpri=10],其 parent 为 null。

流程图如下: 流程图

四、为什么主线程会出现无法响应,而子线程不会

首先我们要知道应用程序和系统间的交互基于的是 消息驱动 的模型:

  • 事件源通过 Binder 调用传递到应用程序进程后,会将处理的消息加入到消息队列当中,例如按键、触摸、绘制等。
  • 应用程序再不断地从消息队列中取出消息进行处理。

应用程序是在其主线程进行处理这些消息的,这里是通过一个无限循环,即 Looper.loop(),没有消息时休眠,有消息时被唤醒去处理消息,因此主线程不能够结束,结束了就没法处理消息了。

即 ActivityThread.main 方法:

public static void main(String[] args) {
Looper.prepareMainLooper();
Looper.loop();
throw new RuntimeException(“Main thread loop unexpectedly exited”);
}

由于主线程结束了,那么就无法及时处理 AMS/WMS 发送的消息或者给予反馈,就会触发系统调用错误或者 ANR。
而子线程由于不涉及这些,结束就结束了,和任务执行完了没什么区别,因此也就不会产生异常。
无、默认的处理规则

假如我们没有设置自定义的 handler,那么系统是有自己一套默认的处理逻辑的,这段代码在应用进程启动时,即 RuntimeInit#commonInit 中:
protected static final void commonInit() {

Android高级架构师

由于篇幅问题,我呢也将自己当前所在技术领域的各项知识点、工具、框架等汇总成一份技术路线图,还有一些架构进阶视频、全套学习PDF文件、面试文档、源码笔记。

  • 330页PDF Android学习核心笔记(内含上面8大板块)

  • Android学习的系统对应视频

  • Android进阶的系统对应学习资料

  • Android BAT部分大厂面试题(有解析)

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值