Catch any exception in Android

Implement custom exception logger (lust like CrashAnalytics)

Here is how you can intercept any exception in android and log it to your external logging system, for better user experience.

final Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler = 
        Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        // TODO, implement your logging here
        // ...
        
        // Now re-throw exception so JVM can behave as expected (i.e. crash your app)
        defaultUncaughtExceptionHandler.uncaughtException(t, e);
    }
});

Last updated