Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using global exception handling with "setUncaughtExceptionHandler" and "Toast"

I am trying to create a simple exception handler which will help me debug the application. Right now, when I have an exception I am forced to connect with Eclipse debugger merely to see the exception details.

To avoid that I've used setUncaughtExceptionHandler to handle any unhandled exception and display a Toast on the exception. Unfortunately, that doesn't work.

public class TicTacToe extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread thread, Throwable ex) {
                Toast.makeText(TicTacToe.this, "TOAST", Toast.LENGTH_LONG).show();
            }
        });

        setContentView(R.layout.main);

        Button continueButton = (Button) findViewById(R.id.cell01);
        continueButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int i = 5;
                i = 5 / 0;

                Toast.makeText(TicTacToe.this, "BUTTON", Toast.LENGTH_LONG).show();             
            }
        });

    }
}

Essentially I made a form with a single button, pressing on which, it would throw a devision-by-zero exception. However, pressing the button doesn't cause the global toast handler to show. Instead, the button stays orange (pressed) and nothing happens.

Needless to say, if I comment out i = 5 / 0; I see the toast that says that a button was pressed.

Two questions: 1) Why isn't the toast showing in the UncaughtExceptionHandler body? How do cause it to show? 2) Is there an alternative/better way for global exception handling? I guess I could install aLogCat on the android simulator and simply log the uncaught exception, it seems, however, less comfortable - I will need to be switching applications just to see exception details.

Thanks!

like image 803
VitalyB Avatar asked Jul 03 '10 11:07

VitalyB


1 Answers

It is possible. You need to do it inside another thread
Then it should be like this

    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            new Thread() {
                @Override
                public void run() {
                    Looper.prepare();  
                    Toast.makeText(TicTacToe.this, "TOAST", toast.LENGTH_LONG).show();
                    Looper.loop();
                }
            }.start();
        }
    });
like image 123
Labeeb Panampullan Avatar answered Oct 13 '22 22:10

Labeeb Panampullan