5

My question is how to catch unhandled errors in Android application and pass them further so it will indeed crash the application.

I'm creating SDK for Android and I still want developers to handle their errors but I also want to get informed about crashes of mine.

I know that to catch an error I could use:

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

        }
    });

But how to pass it futher? How to crash application? If I use:

throw new RuntimeException(ex);

It won't crash application but rather cause ANR error.

The second question is how does Fabric (Crashlytics) library work? Mind that I also don't want to spoil workflow of Fabric if it's also present in the application.

Adam Styrc
  • 1,517
  • 2
  • 23
  • 38
  • I'm sorry, my ANR error was because of other reason so it really crashes the app. – Adam Styrc Feb 02 '16 at 11:32
  • don't forget to exit the application at the end of `uncaughtException`. `Process.killProcess(Process.myPid()); System.exit(1);` – amrezzd Aug 12 '18 at 13:03

2 Answers2

2

In a low level UncaughtExceptionHandler is the mechanism to catch of all your application errors in case if instance of UncaughtExceptionHandler attached to application thread.

How to crash application?

Use this thread

It won't crash application but rather cause ANR error.

This happens because you throw Exception and you going inside uncaughtException method, where you throw exception again. So you have a cycle.

But how to pass it futher?

I suppose you need to save exception data to some storage - SD card, send crash info to email, etc.

In this case you need implement your logic inside uncaughtException method.
You don't need to pass it futher!

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();
        }
    });

You need put Thread.setDefaultUncaughtExceptionHandler(...) to your Application class for the best case.

The second question is how does Fabric (Crashlytics) library work? Mind that I also don't want to spoil workflow of Fabric if it's also present in the application.

Fabric uses also UncaughtExceptionHandler to catch all errors in your app.

If you need to see error in logcat

Just filter logcat by Answers tag. Or by System.exit

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • Good explanation. +1 – Rohit5k2 Feb 02 '16 at 11:21
  • You are right that I don't have to rethrow that error. But my concern is if I override DefaultUncaughtExceptionHandler with: Thread.setDefaultUncaughtExceptionHandler() won't is spoil Fabric ? – Adam Styrc Feb 02 '16 at 11:34
  • Swallowing exceptions in UncaughtExceptionHandler is a dangerous idea. Depending upon the thread original crash occurred on, version of Android and other factors, this results in the app silently closing (without standard user-facing dialog), continuing to run in broken state or producing ANR. See correct answer by CommonsWare. – gmk57 Jul 16 '20 at 19:17
  • And Crashlytics will probably be broken with this approach. It won't get the chance to process the exception. – gmk57 Jul 16 '20 at 19:33
2

But how to pass it futher?

Before calling setDefaultUncaughtExceptionHandler(), call getDefaultUncaughtExceptionHandler(). Hold onto the old uncaught exception handler in your new one. When you are called with uncaughtException(), do your own work and also call uncaughtException() on the old handler.

How to crash application?

I usually divide some number by zero.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I like holding reference to already existing exception handler but what if fabric is initiated AFTER my SDK ? Will it do the same or just erase my handler? – Adam Styrc Feb 02 '16 at 11:37
  • @AdamStyrc: "Will it do the same" -- I have no idea, as I did not write Fabric. You will have to try it yourself or see if the behavior is documented. Moreover, for your own app, you know the order in which you are setting up these handlers. – CommonsWare Feb 02 '16 at 11:43
  • I m only SDK creator, that's why I ask if Fabric won't supress my handler ;) ok, I understand all, thx! – Adam Styrc Feb 02 '16 at 11:50