How do I stop my application from zombifying after I handle an uncaught Excepition?

Go To StackoverFlow.com

6

I am handling any uncaught exceptions that occur in my application via:

Thread.setDefaultUncaughtExceptionHandler(this);

were this is my launcher Activity implementing UncaughtExceptionListener. I handle the exception and send it off to my log server just fine, however, my application then doesn't end. It just runs along as a zombie until the home or back button is pressed. How can I kill the process after handling the exception?

Edit
Here is a test and working activity that throws an uncaught exception that is to be caught (by Thread) and handled. The toast is never posted and the process goes zombie once the exception is logged.

class TestActivity extends Activity implements UncaughtExceptionListener {

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Thread.setDefaultUncaughtExceptionHandler(this);
        throw new RuntimeException("I'm a destuctive booger.");

        setContentView(R.layout.activity_test);
    }

    @Override public void uncaughtException(Thread thread, Throwable toss) {
        Log.e(TAG, "An uncaught exception has been recieved.", toss);
        Toast.makeText(this, "Big Error", Toast.LENGTH_LONG).show();
    }
}
2012-04-05 18:00
by ahodder
Call finish() in the handler method. You'll probably also want to provide a popup dialog to inform the user as well though - Squonk 2012-04-05 18:02
@MisterSquonk, can't, my context is nulled out. I initially had it toasting a critical error message, but the toast won't create. I then tried a broadcast receiver and that didn't work either - ahodder 2012-04-05 18:03
Sorry, I don't understand. You say that this is your launcher Activity so if the handler method is part of an Activity, in order to handle it you must have a 'running' Activity. The fact that Activity is an indirect sub-class of Context means (in OOP terms) an Activity IS a Context - Squonk 2012-04-05 18:12
@MisterSquonk Here, I updated my question with an example that still properly shows my issue - ahodder 2012-04-05 19:28
Well finish() does work but the best I've come up with for alerting the user is to use NotificationManager to send a status bar Notification using getApplicationContext() for the Context. Unfortunately this does mean having to provide a PendingIntent to handle when a user clicks on the notification itself. Sorry, I can't get anything else to work - Squonk 2012-04-06 00:43
@MisterSqounk No, thanks for the help. I do prefer this method. If you post an answer, its yours - ahodder 2012-04-06 14:33
I converted my comment to an answer - Squonk 2012-04-07 23:33


1

I did some testing with your example code and discovered that finish() will work from uncaughtException(). It's silent however so doesn't give any feedback to the user and, like you, I was unable to invoke a Toast or any other visual message box even using getApplicationContext().

The only way of sending any feedback that I was able to get working was to use NotificationManager to send a status bar Notification using getApplicationContext() for the Context.

I didn't spend much time on it but unfortunately it seems this does mean having to provide a PendingIntent to handle when a user clicks on the notification itself. Doing this with the same Activity could obviously risk an infinite loop if the Activity set for the PendingIntent is one which generates an unhandled exception.

It would be possible, however, to create a vary basic Activity perhaps with a dialog theme that would be opened and explain the crash.

2012-04-07 23:32
by Squonk


0

Not sure if there is a gentler way of dealing with this problem, but you may try:

import android.os.Process;

Process.killProcess(Process.myPid());
2012-04-05 19:11
by Dheeraj V.S.
Ads