Best exception handling practices for Android?

If my application crashes, it freezes for a couple of seconds before I tell Android that the application crashed and needs to be closed. So I was thinking about catching all exceptions in my application using general:

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

And create a new Activity that will explain that the application instantly worked (and also provided users with the ability to send mail with error data) instead of delaying this delay thanks to Android. Are there any better methods to achieve this or is it discouraging?

Update: I am using Nexus 5 with ART turned on, and I do not notice the delay that I encountered application crashes (the "freeze" that I spoke about initially). I think, since now everything has its own code, an accident occurs instantly along with getting all the information about the failures. Perhaps the Nexus 5 is just fast :) despite this, it may not be a concern in future releases of Android (given that ART will run by default in Android L).

+62
java android exception-handling
May 15 '13 at 9:42
source share
3 answers

Check the link here for reference .

Here you create a class: ExceptionHandler , which implements java.lang.Thread.UncaughtExceptionHandler ..

Inside this class, you will do your own things, saving lives, for example, by creating stacktrace and gettin, ready to load an error report, etc ...

Now comes the important part . That is how to catch this exception. Although it is very simple. Copy the following line of code into your every activity right after calling the super method in the override onCreate method.

 Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); 

Your activity might 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 ...

+81
May 15, '13 at 10:34
source share

You can simply use the general warning 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 can also add other error handling methods to this method, for example, printtttrace

+8
Aug 09 '13 at 9:40 on
source share

I found the wtf method (what a terrible failure) in the log class. From the description:

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

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

Hope this helps someone

+2
Mar 16 '14 at 15:19
source share



All Articles