Do Not Force Android Users Away, Handle Your Exceptions

A characteristic of android java applications is that we have java exceptions that handle errors when thrown. But, its a chain of responsibility that travels from your lowly class that you just created up the object hierarchy as the java system searches for an Exception class to handle the error.

But, Android applications are no longer a few measly megabytes but now can be larger in hundreds so megabytes in complexity and you cannot always know each exception that will be thrown for every application task and process. And if its left up to the Android framework, it knows nothing of your application and not even how to direct the user to send you a log of the exception thrown.

Android Mobile device users are these weird humans that actually expect that if an error occurs in an application that they be involved in having you fix the error. It even keeps them as a customer of your android application. So would it not make sense to handle that exception you did not plan for?

I am going to show you how to grab that un-planned unhandled exception before it gets sent up to the Android framework and show how to have the user send you a log of that exception.


Let me step you through the code. We have to catch the unhandled exception that might be thrown on the main thread or the non-main thread than we have to show a dialog to the user to send you the log of that exception.

So I extended the Application class and use setting the default uncaughtexceptionhandler on the Thread class to redefine the UncaughtExceptionHandler interface in the Thread class to call the private handleUncaughtException method so that we can direct the user to a dialog to send us the log of the exception thrown.

If the exception is thrown on the non-thread than we post a runnable so that we can have the activity to send us the log launched on the main thread.

Note, that this only part of the android user application retention process. As you still have to follow up and indicate to that user that you received the log report and the progress on fixing that error that crashed the application.