Home » Android » exception handling – Android : avoid crashing of app due to unhandled errors

exception handling – Android : avoid crashing of app due to unhandled errors

Posted by: admin June 15, 2020 Leave a comment


In my Android app I have tried to put Try Catch blocks in all possible places. However I want to avoid the crashing of the app due to any unhandled errors. How can I achieve that?

I have used Thread.setDefaultUncaughtExceptionHandler(handler); but that will just help me to obtain crash- data right?

How to&Answers:

Why do you want to do this?

If there are places where you can catch an exception and do something meaningful, i.e. display a useful warning and then continue with the application in a consistent and usable state, then fine.

If you can’t take any meaningful action, then just let the failure happen. There are plenty of ways you can be notified of the resulting failures, so you can fix them: have a look at ACRA, for example. Or, the Android Developer console will now report failure of your Market-distributed apps.


you can use the following way :

public class MyApplication extends Application
  public void onCreate ()
    // Setup handler for uncaught exceptions.
    Thread.setDefaultUncaughtExceptionHandler (new Thread.UncaughtExceptionHandler()
      public void uncaughtException (Thread thread, Throwable e)
        handleUncaughtException (thread, e);

 // here you can handle all unexpected crashes 
  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

that will handle your app unexpected crashes, this taken from that answer.


I suggest you read about ACRA here


All errors and exceptions extend from Throwable. By catching Throwable, it is possible to handle all unexpected conditions. But catching Errors would have noresults. You just can do smth in catch block before application crash


Have a look on
Best Practices for Exception handling in android