Skip to content

Instantly share code, notes, and snippets.

@piyush-malaviya
Created December 4, 2019 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piyush-malaviya/ecd92eac12ed00c17c26d4b675a0c464 to your computer and use it in GitHub Desktop.
Save piyush-malaviya/ecd92eac12ed00c17c26d4b675a0c464 to your computer and use it in GitHub Desktop.
Global Exception Handling on android
Stackoverflow Link: https://stackoverflow.com/a/19968400
Handle uncaughtException, start activity:
public class MyApplication extends Application
{
public void onCreate ()
{
// Setup handler for uncaught exceptions.
Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
{
@Override
public void uncaughtException (Thread thread, Throwable e)
{
handleUncaughtException (thread, e);
}
});
}
public void handleUncaughtException (Thread thread, Throwable e)
{
e.printStackTrace(); // not all Android versions will print the stack trace automatically
Intent intent = new Intent ();
intent.setAction ("com.mydomain.SEND_LOG"); // see step 5.
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); // required when starting from Application
startActivity (intent);
System.exit(1); // kill off the crashed app
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment