3

I am creating a Launcher. Sometimes I have errors and of course I have to fix them but I am trying to use an UncaughtExceptionHandler for my whole app.

To do that I use this class:

  public class clsMyApplication extends Application 
  {
     // uncaught exception handler variable
     private UncaughtExceptionHandler defaultUEH;

     // handler listener
     private Thread.UncaughtExceptionHandler _unCaughtExceptionHandler = new Thread.UncaughtExceptionHandler() 
     {  @Override public void uncaughtException(Thread thread, Throwable ex) 
        {
           try
           {  Save a log file with the error and 
              save preferences indicating there was an error to display the next start up.
           }
           catch(Exception e)
           {

           }



           //Alarm to restart app:
           PendingIntent myActivity = PendingIntent.getActivity(clsMyApplication.this, 192837, new Intent(clsMyApplication.this, clsMyMainActivity.class), PendingIntent.FLAG_ONE_SHOT);
           AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
           alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 5000, myActivity );
           System.exit(2);


           // re-throw critical exception further to the os (important)
           defaultUEH.uncaughtException(thread, ex);
        }
     };

     public clsMyApplication() 
     {  
        defaultUEH = Thread.getDefaultUncaughtExceptionHandler();

        // setup handler for uncaught exception 
        Thread.setDefaultUncaughtExceptionHandler(_unCaughtExceptionHandler);      
     }


  }

I also add this class to the manifest like this:

<application       
    android:name=".clsMyApplication"         
    ....

But I still get the Android Exceptions. I am not able to get them using this class. Any help?

Ton
  • 9,235
  • 15
  • 59
  • 103
  • Did you try overriding the onCreate() instead of using a constructor? (i.e. replace `public clsMyApplication()` by `public void onCreate()`) – Gorcyn Mar 03 '15 at 16:39
  • possible duplicate of [Using Global Exception Handling on android](http://stackoverflow.com/questions/4427515/using-global-exception-handling-on-android) – Gorcyn Mar 03 '15 at 16:41
  • I tried as you suggested but it didn't work – Ton Mar 03 '15 at 17:26

2 Answers2

2

Add this line of code inside of any method (preferrably inside an OnCreate activity)

Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable t) {
            //Do something
        }
    });
programmer23
  • 533
  • 4
  • 15
1

I guess probably that you didn't handle the exception in the override method uncaughtException(Thread thread, Throwable t){}

In my project usually I would handle those unexpected exceptions here rather than handling by system.

@Override
public void uncaughtException(Thread thread, Throwable ex) {
    if (!handleException(ex) && mDefaultHandler != null) {
        // default exception handler
        mDefaultHandler.uncaughtException(thread, ex);
    } else {
        // exit program
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
mmgross
  • 3,064
  • 1
  • 23
  • 32
Snk
  • 11
  • 3
  • However, I couldn't read any err logs in logcat if I did it this way, while you can find err logs in warn instead of error. – Snk Apr 10 '15 at 02:36