Android : avoid crashing of app due to unhandled errors

you can use the following way :

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);
      }
    });
  }

 // 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.


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.


I suggest you read about ACRA here