4

In my Android application I utilize setDefaultUncaughtExceptionHandler to store information about unhandled exceptions locally on a user device. After some feedback I suspect that this code prevents the built-in Google's error-reporting feature from work, because I do not see error reports in the developer console, while exceptions are reported by users. Their devices are well past 2.2, where the error-reporting was introduced. Could it be that specific device with, say, 4.0.3 does not support this feature? If yes, how can I detect this programmatically?

I can't find information regarding this in Android documentation. I'd like both standard error-reporting and my custom handling work together. In my custom exception handler I call Thread.getDefaultUncaughtExceptionHandler() to get default handler, and in my implementation of uncaughtException I propagate exception to this default handler as well.

Stan
  • 8,683
  • 9
  • 58
  • 102

2 Answers2

3

I first tried calling System.exit(1); as mentioned in this SO answer, but that didn't work.

Finally solved it by calling the uncaughtException(Thread thread, Throwable ex) again on Androids default UncaughtExceptionHandler (found it by checking the ACRA source code.

Example Activity

public class MainActivity extends Activity implements Thread.UncaughtExceptionHandler {
    private Thread.UncaughtExceptionHandler _androidUncaughtExceptionHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _androidUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);

        // Rest onCreate
        setContentView(R.layout.main_activity);
    }

    //@Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            // Do your stuff with the exception
        } catch (Exception e) {
            /* Ignore */
        } finally {
            // Let Android show the default error dialog
            _androidUncaughtExceptionHandler.uncaughtException(thread, ex);
        }
    }
}
Community
  • 1
  • 1
Kapé
  • 4,411
  • 3
  • 37
  • 54
0

Yes, this will stop the inbuilt error report. The user is given a dialog when your app crashes, with an option to report the error via Google Play. However, if you use setDefaultUncaughtExceptionHandler() then the exception is handled within your app, and no option is given to report it.

I recommend that you integrate ACRA into your project, as it allows you to easily receive error reports upon crashes.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • Hm, do you mean ACRA will allow to see errors both in the developer console and in a custom way? I'd like to preserve standard error-reporting. As to sending reports, I have this custom functionality as well, but I thought it's excessive because of Google's standard feature. – Stan Sep 21 '12 at 19:06
  • No. ACRA will not show them in the developer console, but it provides very very detailed reports, containing far more information than the dev console ones – Raghav Sood Sep 21 '12 at 19:10
  • According to this [SO](http://stackoverflow.com/a/11281388/465942 "setDefaultUncaughtExceptionHandler makes app crash silently") question, calling `System.exit(1);` should work, but I haven't got any luck with it unfortunately.. – Kapé Sep 11 '14 at 22:02