Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android exception handling best practice?

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).

like image 466
ldam Avatar asked May 15 '13 09:05

ldam


People also ask

How do I handle exceptions in Android?

How Exceptions Work in JVM and Android. In Java, all exception and error types are subclasses of Throwable. The Throwable class changes the execution flow of JVM apps by throwing exceptions and deciding how to recover from an error. Adding an exception to a code changes its execution flow.

Which of the option is best suited for the exception handling?

Option (B) is correct. Question 10 Explanation: Throwable class is the built-in base class used to handle all the exceptions in Java.

What are exceptions of Android?

An exception thrown when an error occurs during parsing. The object you are calling has died, because its hosting process no longer exists. The core Android system has died and is going through a runtime restart. Exception thrown when the provisioning server or key server denies a certficate or license for a device.


2 Answers

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...

like image 185
CRUSADER Avatar answered Sep 17 '22 19:09

CRUSADER


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

like image 43
Louis Evans Avatar answered Sep 16 '22 19:09

Louis Evans