9

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?

Tushar Vengurlekar
  • 7,649
  • 8
  • 33
  • 48

5 Answers5

7

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.

Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • why do you kill the app in the end via `System.exit()`? Isn't it possible to just let it continue to run if the crash was something that does not affect the view or the functionality? – avalancha Jun 11 '20 at 12:01
  • @avalancha sorry just noticed your comment, actually it's killing the current instance and release a new app instance (as I remember :D) – Muhammed Refaat Jan 15 '22 at 19:14
2

I suggest you read about ACRA here

mah
  • 39,056
  • 9
  • 76
  • 93
1

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.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • 5
    @Glenn, "Why do you want to do this?" is a perfectly valid response. An answer doesn't have to be a direct answer to the question. It can be in the form of "you shouldn't do that, have you tried considering this instead?" (see the SO FAQ). In this situation, swallowing all exceptions is quite a bad practice. It's not a good idea to catch everything and allow your app to "muddle on through" given whatever inconsistent state it happens to be in. It's better to deal with specific situations where you can give meaningful feedback to the user or take meaningful action about what went wrong. – Doctor Jones Aug 20 '13 at 14:33
  • 12
    It's a response. It's not an answer. – Glenn Maynard Aug 20 '13 at 16:06
0

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

amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

Have a look on Best Practices for Exception handling in android

Thanks Deepak

Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243