Rethrow UncaughtExceptionHandler Exception after Logging It-Collection of common programming errors

Apologies, not an Android expert – but looks like you can’t throw ex because your method signature “void uncaughtException(Thread, Throwable)” doesn’t declare that it “throws” anything.

Assuming you’re overriding an API interface and (a) can’t modify this signature and (b) don’t want to because you’d be throwing it out of context, could you instead use a decorator pattern and basically subclass the default UncaughtExceptionHandler implementation to log your message and then let it carry on processing as usual?

Edit: untested, but this might look a bit like:

    final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            // your code 
            AnalyticsUtils.getInstance(MainApplication.this).trackEvent(
                    "Errors",                       // Category
                    "MainActivity",                 // Action
                    "Force Close: "+ex.toString(),  // Label
                    0);                             // Value
            AnalyticsUtils.getInstance(MainApplication.this).dispatch();
            Toast.makeText(MainApplication.this, "Snap! Something broke. Please report the Force Close so I can fix it.", Toast.LENGTH_LONG);

            // carry on with prior flow
            subclass.uncaughtException(thread, ex);
        }
    });