Framework 源码解析 (8) - UncaughtExceptionHandler 小结

一、前言

当我们的应用程序在 Java 层发生异常时,可以使用 Thread.setDefaultUncaughtExceptionHandler 来进行全局异常的捕获,这篇文章回答了下面四个问题:

  • 在主进程中的主线程、子线程发生崩溃的表现?
  • UncaughtExceptionHandler 是由谁调用的?
  • 为什么同样都是捕获了异常,主线程会出现异常或者无法响应,而子线程依然可以正常后续的操作?
  • 不设置 UncaughtExceptionHandler 时,系统默认的处理逻辑是什么?

二、现象

首先在 Application 中设置:

class ClientApplication: Application() {

    companion object {
        val TAG: String = ClientApplication::class.java.name
    }

    override fun onCreate() {
        super.onCreate()
        Thread.setDefaultUncaughtExceptionHandler {
            thread, e -> Log.d(TAG, "thread=" + thread.id + ",throwable=" + e.message)}
    }
}

2.1 主进程 + 主线程

class ClientActivity : AppCompatActivity() {

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

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

}

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

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<Button>(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() {
        LoggingHandler loggingHandler = new LoggingHandler();
        RuntimeHooks.setUncaughtExceptionPreHandler(loggingHandler);
        Thread.setDefaultUncaughtExceptionHandler(new KillApplicationHandler(loggingHandler));
}

KillApplicationHandler 会通过 Binder 调用到 AMS 端进行异常信息的记录,最后会调用 Process.killProcess(Process.myPid()) & System.exit(10) 结束掉应用进程,这也是为什么在没有设置自定的 handler 情况下,子线程当中发生异常会导致应用程序退出。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容