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).
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.
Option (B) is correct. Question 10 Explanation: Throwable class is the built-in base class used to handle all the exceptions in Java.
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.
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...
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
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