Android – how to catch unhandled errors and pass them further

My question is how to catch unhandled errors in Android applications and pass them further to other applications so as to actually crash the application

I'm creating an SDK for Android, but I still want developers to deal with their errors, but I also want to know about my crash

I know that to catch errors, I can use:

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {

        }
    });

However, how to further pass it? How do I crash an application? If I use:

throw new RuntimeException(ex);

It will not crash the application, but will cause anr errors

The second question is how does the fabric (crashlytics) library work? Remember, if fabric workflow also exists in the application, I don't want to destroy it

resolvent:

At a lower level, uncaughtexceptionhandler is a mechanism to catch all application errors when an uncaughtexceptionhandler instance is attached to an application thread

Use this thread

This happens because you throw an exception and enter the uncaughtexception method, in which the exception is thrown again. So you have a cycle

I think you need to save abnormal data to some storage area - SD card, send crash information to email, etc

In this case, you need to implement the logic in the uncaughtexception method. You don't need to pass it further!

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
               // put your save logic here
               // save to file, send to email, etc.
               // Also you can get information about throwed exception
               // for example : ex.getMessage();
        }
    });

It is better to put thread. Setdefaultuncaughtexceptionhandler (...) into the application class

Fabric also uses uncaughtexceptionhandler to catch all errors in the application

If you need to see an error in logcat

Just filter logcat. Through the answers tag or system.exit

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>