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?
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.
I suggest you read about ACRA here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With