-1

I have implemented a uncaught exception handler to catch uncaught exceptions. But some time or in some devices this handler failed with java.lang.StackOverflowError. Here is logcat of exception:

java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:121)
at java.lang.StringBuffer.append(StringBuffer.java:278)
at java.io.StringWriter.write(StringWriter.java:123)
at com.android.internal.util.FastPrintWriter.flushLocked(FastPrintWriter.java:358)
at com.android.internal.util.FastPrintWriter.appendLocked(FastPrintWriter.java:303)
at com.android.internal.util.FastPrintWriter.write(FastPrintWriter.java:625)
at com.android.internal.util.FastPrintWriter.append(FastPrintWriter.java:658)
at java.io.PrintWriter.append(PrintWriter.java:691)
at java.io.PrintWriter.append(PrintWriter.java:31)
at java.lang.Throwable.printStackTrace(Throwable.java:324)
at java.lang.Throwable.printStackTrace(Throwable.java:300)
at android.util.Log.getStackTraceString(Log.java:459)
at com.example.src.CustomExceptionHandler.uncaughtException(CustomExceptionHandler.java:36)
at com.example.src.CustomExceptionHandler.uncaughtException(CustomExceptionHandler.java:41)
at com.example.src.CustomExceptionHandler.uncaughtException(CustomExceptionHandler.java:41)

And the line that cause error defaultUEH.uncaughtException(t, e); according to stacktrace.

And full code is:

import java.lang.Thread.UncaughtExceptionHandler;
import android.util.Log;

class CustomExceptionHandler implements UncaughtExceptionHandler {

private UncaughtExceptionHandler defaultUEH;

protected CustomExceptionHandler() {
    try
    {
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }
    catch(Exception e)
    {
        System.out.println("Exception: "+e);
    }
}

public void uncaughtException(Thread t, Throwable e) {
    try
    {
        String stacktrace = Log.getStackTraceString(e);

        SendStackTraceToServer sendStackTraceToServer = new SendStackTraceToServer();
        sendStackTraceToServer.startThreadToSendRequest(stacktrace);

        defaultUEH.uncaughtException(t, e); // this line cause stackoverflow error.
    }
    catch(Exception ex)
    {
        System.out.println("Exception: "+ex);
    }
}

protected static void setDefaultUncaughtExceptionHandler()
{
    Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());
}
}

Help me solve this issue. Because I am not able to reproduce this issue (or I don't know how to reproduce this issue).

DevK
  • 145
  • 2
  • 13

2 Answers2

0

java.lang.StackOverflowError is Error not Exception

http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html

You can catch Throwable if you need to catch all "throwable"

Vitaly
  • 2,552
  • 2
  • 18
  • 21
  • Anyway it is bad idea to catch it – Vitaly Jan 15 '15 at 06:59
  • OK. Do you have any idea why "UncaughtExceptionHandler" cause stackoverflow error at line defaultUEH.uncaughtException(t, e);. What is the reason of application recurses too deeply. – DevK Jan 15 '15 at 07:05
0

defaultUEH.uncaughtException(t, e); invokes defaultUEH.uncaughtException(t, e); invokes defaultUEH.uncaughtException(t, e); ... and in the end you get StackOverflowError.

May be I am not right, but I think your code is complex.

I recommend use Handler like this:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        String stacktrace = Log.getStackTraceString(e);
        SendStackTraceToServer sendStackTraceToServer = new SendStackTraceToServer();
        sendStackTraceToServer.startThreadToSendRequest(stacktrace);
    }
});
DmitryKanunnikoff
  • 2,226
  • 2
  • 22
  • 35