Replacing the default exception handler not used to prevent crashes

We wanted to replace the unused default exception so that the default failure dialog does not appear.

The problem was that if you call Thread.setDefaultUncaughtExceptionHandler(YourHandler) , then in case of an exception, the application freezes and you get an ANR dialog (the application does not respond). We experimented with System.exit() and Process.killProcess() , which solved the problem, but, reading on this issue, it seemed like it was discouraging.

So how can this be done correctly?

+1
source share
1 answer

TL DR

To accept the code in the implementation of the implementation of the standard exception handler in com.android.internal.os.RuntimeInit.UncaughtHandler , omitting that shows the dialog.

Drilldown

At first, it is clear that System.exit() and Process.killProcess() are required in a scenario where the application crashes (at least in the way that people on Google think).
It is important to note that com.android.internal.os.RuntimeInit.UncaughtHandler can (and can) change between releases of the framework, and also some code in it is not available for your own implementation.
If you are not interested in the default crash dialog and just want to add something to the default handler, you must migrate the default handler. (see below for example)

Our default Uncaught exception handler (sans dialog)

 Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable ex) { try { // Don't re-enter -- avoid infinite loops if crash-reporting crashes. if (mCrashing) { return; } mCrashing = true; String message = "FATAL EXCEPTION: " + t.getName() + "\n" + "PID: " + Process.myPid(); Log.e(TAG, message, ex); } catch (Throwable t2) { if (t2 instanceof DeadObjectException) { // System process is dead; ignore } else { try { Log.e(TAG, "Error reporting crash", t2); } catch (Throwable t3) { // Even Log.e() fails! Oh well. } } } finally { // Try everything to make sure this process goes away. Process.killProcess(Process.myPid()); System.exit(10); } } 

})

Default Handler Wrap

 final Thread.UncaughtExceptionHandler defHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread t, Throwable ex) { try { //your own addition } finally { defHandler.uncaughtException(t, ex); } } }); 
+2
source

All Articles