UncaughtExceptionHandler中的StackOverflowError

时间:2015-01-15 06:49:34

标签: java android uncaughtexceptionhandler

我已经实现了一个未捕获的异常处理程序来捕获未捕获的异常。但是有些时候或某些设备中这个处理程序失败了java.lang.StackOverflowError。这是异常的logcat:

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)

根据stacktrace导致错误defaultUEH.uncaughtException(t, e);的行。

完整的代码是:

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());
}
}

帮我解决这个问题。因为我无法重现此问题(或者我不知道如何重现此问题)。

2 个答案:

答案 0 :(得分:0)

java.lang.StackOverflowError是错误不是异常

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

如果你需要抓住所有“扔掉的”

,你可以抓住Throwable

答案 1 :(得分:0)

defaultUEH.uncaughtException(t,e);调用defaultUEH.uncaughtException(t,e);调用defaultUEH.uncaughtException(t,e); ...最后你会得到StackOverflowError。

可能我不对,但我认为你的代码很复杂。

我建议像这样使用Handler:

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);
    }
});