12

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!

Ryan Conrad
  • 6,870
  • 2
  • 36
  • 36
VitalyB
  • 12,397
  • 9
  • 72
  • 94
  • If you put Log.e("TicTacToe","Unhandled Exception",ex) in there instead of the Toast, and view the logcat in like eclipse or with DDMS, does it log the error? – Ryan Conrad Jul 03 '10 at 13:25
  • 1
    You don't need debugger, just the Logcat view. And you'll need Eclipse to fix the problems anyways. – yanchenko Jul 03 '10 at 16:19
  • Woah @ Alex. I was sure I had to be connected to see aLogCat. Thanks a lot! – VitalyB Jul 03 '10 at 18:12
  • @ Ryan: Yes, it does appear in LogCat then. I think Qberticus came up with the reason. – VitalyB Jul 03 '10 at 22:54

3 Answers3

14

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();
        }
    });
Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • @sheetal i am facing the same issue. Did you find a fix for the same? Thanks in advance – Anish May 22 '15 at 05:56
  • No you cant...For starting another activity from this thread needs the activity to be declared as a different process in Manifest – sheetal May 22 '15 at 15:21
11

You're not seeing anything because the exception happened on your UI thread and the stack unrolled all the way. So there is no more Looper and there is no support there that is used to display the Toast. If you want to display the exception information on screen you'll most likely need to start another Activity in another process.

There is also a problem with your UncaughtExceptionHandler. You really should keep a reference to the old one and call it at the end of uncaughtException this allows the system to display the Force Close button.

Rich Schuler
  • 41,814
  • 6
  • 72
  • 59
  • Thanks, that explains it. Does it mean that this process is done for? I can't stop it from closing down at that point? – VitalyB Jul 03 '10 at 22:55
  • Yea, pretty much. The default uncaught exception handler tries to kill the process. I'm thinking they do that for a good reason and you should call it and let it do that. – Rich Schuler Jul 04 '10 at 00:34
  • You mean that there isn't possible to show the alert dialog in this activity? – Gratzi Dec 13 '10 at 11:57
  • 3
    Qberticus: "you'll most likely need to start another Activity in another process."... I just can't find how to do that! I've googled it, with no success. Can you post some code? – Christian May 03 '11 at 22:33
1

I know it's an old question but I hope I can save someone from frustration and wasting time.

Qberticus is right, you can't start an Activity on the same process, but you can kill the current process and have android run it in a new one:

Intent intent = new Intent(myContext, AnotherActivity.class);
intent.putExtra("error", errorReport.toString());
myContext.startActivity(intent);

android.os.Process.killProcess(android.os.Process.myPid());
System.exit(10);

refer to this page for an awesome working example:

Iman Akbari
  • 2,167
  • 26
  • 31