Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all possible android exception globally and reload application

Tags:

I know the best way to prevent system crashes is catching all possible exception in different methods. So I use try catch blocks every where in my code. However as you know sometimes you forget to test some scenarios which cause some unhanded exceptions and a user gets "Unfortunately App stopped working..." message. This is bad for any application. Unfortunately the people who will use my app are not native English, so they will not understand crash message too.

So I want to know is it possible to catch all possible exception globally ( with just one try catch block in some main classes not all classes and methods!!!) and reload application automatically and without any weird messages? Or at least is it possible to change the crash message?

Thanks.

like image 269
Taher Avatar asked Aug 26 '15 14:08

Taher


1 Answers

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);             }         }); 
like image 64
Broak Avatar answered Sep 20 '22 23:09

Broak