Skip to content

Instantly share code, notes, and snippets.

@zerobranch
Created October 4, 2018 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerobranch/0740290775f2c0d3dff3a56f9e726cec to your computer and use it in GitHub Desktop.
Save zerobranch/0740290775f2c0d3dff3a56f9e726cec to your computer and use it in GitHub Desktop.
Class allows to catch all exceptions
import android.app.Application;
import timber.log.Timber;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if (BuildConfig.DEBUG) {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(Timber::e));
}
}
}
public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
private Thread.UncaughtExceptionHandler oldHandler;
private UncaughtExceptionCallback action;
public UncaughtExceptionHandler(UncaughtExceptionCallback action) {
this.action = action;
oldHandler = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread thread, Throwable throwable) {
action.uncaughtException(throwable);
if (oldHandler != null) {
oldHandler.uncaughtException(thread, throwable);
}
}
public interface UncaughtExceptionCallback {
void uncaughtException(Throwable throwable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment