异常捕捉:UncaughtExceptionHandler

QuibblerQuibbler 2020-04-29 约 7 分钟 2062 次阅读

异常捕捉:UncaughtExceptionHandler

 1、UncaughtExceptionHandler

UncaughtExceptionHandler是定义在Thread类中的接口,用于处理导致线程终止的未捕获异常。

/**
     * 当线程由于未捕获的异常突然终止时调用的处理程序的接口。 当线程由于未捕获的异常而将终止时,
     * Java虚拟机将使用getUncaughtExceptionHandler查询线程的UncaughtExceptionHandler并将调用处理程序的uncaughtException方法,
     * 并将线程和异常作为参数传递.
     * 如果未显式设置线程的UncaughtExceptionHandler,则其ThreadGroup对象将充当其UncaughtExceptionHandler.
     * 如果ThreadGroup对象对处理异常没有特殊要求,则可以将调用转发到getDefaultUncaughtExceptionHandler默认未捕获的异常处理程序.
     *
     * @see #setDefaultUncaughtExceptionHandler
     * @see #setUncaughtExceptionHandler
     * @see ThreadGroup#uncaughtException
     * @since 1.5
     */
    @FunctionalInterface
    public interface UncaughtExceptionHandler {
    
        /**
         * Method invoked when the given thread terminates due to the
         * given uncaught exception.
         * <p>Any exception thrown by this method will be ignored by the
         * Java Virtual Machine.
         * @param t the thread
         * @param e the exception
         */
        void uncaughtException(Thread t, Throwable e);
        
    }


2、自定义UncaughtExceptionHandler

只需要实现Thread.UncaughtExceptionHandler接口,实现uncaughtException(Thread t, Throwable e)方法,应用遇到异常奔溃的时候,就会调用uncaughtException(Thread t, Throwable e)方法。这里有通用的模板,用Kotlin的object关键字可以很方便的实现一个单例:

object CrashHandler : Thread.UncaughtExceptionHandler {

    override fun uncaughtException(t: Thread, e: Throwable) {
        TODO("1.Report crash 2.Restart App 3.Save logs 4.Kill process")
    }
    
    /**
     * set UncaughtExceptionHandler setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
     *
     * @param thread Thread
     */
    fun setCrashHandler(thread: Thread) {
        thread.uncaughtExceptionHandler = this
    }
    
    public fun init() {
        Thread.setDefaultUncaughtExceptionHandler(this)
    }
    
}

可以在uncaughtException(Thread t, Throwable e)方法中处理异常,比如上报/保存异常信息,重启应用(要避免循环奔溃)等操作。

3、使用UncaughtExceptionHandler

在自定义的Application中,设置自定义的UncaughtExceptionHandler:

class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        
        //给当前应用主线程设置UncaughtExceptionHandler 
        Thread.currentThread().uncaughtExceptionHandler = CrashHandler
        
        //或者给Thread全局设置一个默认的UncaughtExceptionHandler,替换Android框架设置的KillApplicationHandler
        //不管是主线程还是子线程,所有线程奔溃都是使用此UncaughtExceptionHandler
        Thread.setDefaultUncaughtExceptionHandler(CrashHandler)
        
        //或者用init()方法初始化
        CrashHandler.init()
    }
    
}

①catch的异常捕获不到

②只能处理未捕获的异常

参考博客:

       Android利用Thread.UncaughtExceptionHandler收集异常信息

       UncaughtExceptionHandler异常捕获及最佳实践

       UncaughtExceptionHandler 捕捉异常

相关推荐

精选
获取系统SDK版本、判断手机ROM
Code

获取系统SDK版本、判断手机ROM

Build获取系统SDK版本Android中部分API的使用,需要在特定的SDK版本之后才能使用,因此在兼容老版本SDK的时候,经常需要判断API的版本。各种Android版本的对应关系参考《Android各版本对应的SDK版本》判断手机ROM有时候需要判断手机系统的ROM,检测ROM是MIUI、EMUI还是Flyme,可以使用getprop命令,去系统build.prop文件(关于build.p

3.7k
精选
AndroidStudio中各种中文乱码问题
Code

AndroidStudio中各种中文乱码问题

AndroidStudio中各种中文乱码问题1、编译Java错误信息乱码在出现这个Annotation processors must be explicitly declared now...问题的时候,正好也发现AndroidStudio Build Output错误信息都乱码。经常遇到各种问题,习惯了,现在遇到的问题,都是以后的答案。 1.1、修改项目build.gradle(无效)在整个p

2.4k
AndroidStudio 报错:has no declaration in the base values folder
Code

AndroidStudio 报错:has no declaration in the base values folder

在资源文件中正常定义的值,也能编译运行。昨天还正常,今天一打开就报错。应该又是AndroidStudio自身的Bug了。原因众说纷纭,参考Stack Overflow 一篇类似的讨论。解决:File => Invalidate Caches / Restart => Invalidate and Restart.

4.4k