To be able to catch any exception you may encounter that isn’t part of a try-catch-clause, you can use the Thread.setDefaultUncaughtExceptionHandler( Thread. UncaughtExceptionHandler ) method.
In the handler you can then perform whatever action you want. Don’t forget to call “System.exit()” though, because otherwise this won’t work!
A simple example:
- public class SomeActivity extends Activity {
- private static final String LOG_TAG = "SomeActivity";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
- @Override
- public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
- Log.e(LOG_TAG, "OMG! Uncaught Exception!");
- // Without calling System.exit() this will not work.
- System.exit(2);
- }
- });
- }
- }
For some more details have a look at “Using Global Exception Handling on android” on StackOverflow.com.
ready to use
http://www.pipiscrew.com/2013/08/android-global-exception-handler/
learned & thank you