Created
October 4, 2018 08:36
-
-
Save zerobranch/0740290775f2c0d3dff3a56f9e726cec to your computer and use it in GitHub Desktop.
Class allows to catch all exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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