Search code examples
androidstack-traceuncaughtexceptionhandler

UncaughtExceptionHandler issue


In the following code I am obtaining exception data in an released app. This app is not running in debug mode - it is running as a released app on lots of phones.

What I have here works just fine. It puts exception data into shared memory which I mail to myself the next time the app runs. My problem is that the data I get isn't terribly useful. This is what is produced . . .

divide by zero StackTrace= [Ljava.lang.StackTraceElement;@7734137 Cause= null ex= java.lang.ArithmeticException: divide by zero

private Thread.UncaughtExceptionHandler onBlooey = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(Thread thread, Throwable ex) {
                putPref("storedexception", "Exception: " + ex);
        }
};   

This doesn't tell me much. Is there anyway I can get a stacktrace? Anything that would tell me where in the program the error is occurring? Thanks, Dean


Solution

  • Oops. Posted to soon. Figured out the answer...

        private Thread.UncaughtExceptionHandler onBlooey = new Thread.UncaughtExceptionHandler() {
            public void uncaughtException(Thread thread, Throwable ex) {
                    // quickly store the exception info and mail it to me nextime on startup
                debugLog("found uncaught exception: " + ex, true);
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                ex.printStackTrace(pw);
                String st = "";
                st = sw.toString(); // stack trace as a string
                putPref("storedexception", "Exception: " + ex + " stacktrace: " + st);
            }
    };