Android exception handling best practice?

JavaAndroidException Handling

Java Problem Overview


If my app crashes, it hangs for a couple of seconds before I'm told by Android that the app crashed and needs to close. So I was thinking of catching all exceptions in my app with a general:

try {
    // ... 
} catch(Exception e) { 
    // ...
} 

And make a new Activity that explains that the application crashed instantly (and also giving users an opportunity to send a mail with the error details), instead of having that delay thanks to Android. Are there better methods of accomplishing this or is this discouraged?

Update: I am using a Nexus 5 with ART enabled and I am not noticing the delay I used to experience with apps crashing (the "hanging" I was talking about originally). I think since everything is native code now, the crash happens instantly along with getting all the crash information. Perhaps the Nexus 5 is just quick :) regardless, this may not be a worry in future releases of Android (given that ART is going to be the default runtime in Android L).

Java Solutions


Solution 1 - Java

Here, check for the link for reference.

In here you create a class say ExceptionHandler that implements java.lang.Thread.UncaughtExceptionHandler..

Inside this class you will do your life saving stuff like creating stacktrace and gettin ready to upload error report etc....

Now comes the important part i.e. How to catch that exception. Though it is very simple. Copy following line of code in your each Activity just after the call of super method in your overriden onCreate method.

Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

Your Activity may look something like this…

public class ForceClose extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));

        setContentView(R.layout.main);
    }
}

Hope this helps...

Solution 2 - Java

You could just use a generic alert dialog to quickly display error messages. For example...

//******************************************
//some generic method
//******************************************
private void doStuff()
{		
	try
	{
        //do some stuff here
	}
	catch(Exception e)
	{
		messageBox("doStuff", e.getMessage());
	}
}


//*********************************************************
//generic dialog, takes in the method name and error message
//*********************************************************
private void messageBox(String method, String message)
{
	Log.d("EXCEPTION: " + method,  message);

	AlertDialog.Builder messageBox = new AlertDialog.Builder(this);
	messageBox.setTitle(method);
	messageBox.setMessage(message);
	messageBox.setCancelable(false);
	messageBox.setNeutralButton("OK", null);
	messageBox.show();
}

You could also add other error handling options into this method, such as print stacktrace

Solution 3 - Java

i found the "wtf" (what a terrible failure) method in the Log class. From the description:

> Depending on system configuration, a report may be added to the > DropBoxManager and/or the process may be terminated immediately with > an error dialog.

http://developer.android.com/reference/android/util/Log.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionldamView Question on Stackoverflow
Solution 1 - JavaCRUSADERView Answer on Stackoverflow
Solution 2 - JavaLouis EvansView Answer on Stackoverflow
Solution 3 - JavasunyataView Answer on Stackoverflow