Using Global Exception Handling on Android

Catch all possible android exception globally and reload application

In your onCreate

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
//Catch your exception
// Without System.exit() this will not work.
System.exit(2);
}
});

Ideal way to set global uncaught exception Handler in Android

That should be all you need to do. (Make sure you cause the process to halt afterward -- things could be in an uncertain state.)

The first thing to check is whether the Android handler is still getting called. It's possible that your version is being called but failing fatally and the system_server is showing a generic dialog when it sees the process crash.

Add some log messages at the top of your handler to see if it's getting there. Print the result from getDefaultUncaughtExceptionHandler and then throw an uncaught exception to cause a crash. Keep an eye on the logcat output to see what's going on.

Global Exception Handlers in Java

  • The name is a bit misleading, because using that method will set a default exception handler for all threads.
  • Make sure no exceptions can be thrown from your exception handler.
  • If you're doing GUI stuff from your exception handler, make sure you're doing it from the right thread.
  • An uncaught exception will only stop the thread where the exception took place, if that also causes the process to terminate depends on any other threads that might be running.


Related Topics



Leave a reply



Submit