java - 线程退出并出现未捕获的异常 : NO stack trace

标签 java android stack-overflow stack-trace uncaught-exception

我的应用程序导致某处强制关闭,但我的 LogCat 中没有使用通常的(并且非常有用的)堆栈跟踪获得致命异常,我只收到以下 4 行:

06-27 07:08:54.546: D/dalvikvm(14351): GC_FOR_MALLOC freed 9923 objects / 657416 bytes in 21ms
06-27 07:08:54.769: W/dalvikvm(14351): threadid=20: thread exiting with uncaught exception (group=0x4001d7f0)
06-27 07:08:54.796: W/dalvikvm(14351): threadid=21: thread exiting with uncaught exception (group=0x4001d7f0)
06-27 07:08:54.796: I/Process(14351): Sending signal. PID: 14351 SIG: 9

这是在 DEBUG 模式下,没有在 LogCat 上应用过滤器!

  • 是什么导致了这种行为?
  • 有没有办法判断是什么导致了这个异常?

更新:感谢下面的@assylias,我已经能够实现:

final UncaughtExceptionHandler subclass = Thread.currentThread().getUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread paramThread, Throwable paramThrowable) {
    Log.getStackTraceString(paramThrowable);

    subclass.uncaughtException(paramThread, paramThrowable);
    }
});

生成这些添加的行:

06-27 08:24:47.105: D/dalvikvm(15475): GC_FOR_MALLOC freed 13865 objects / 1435952 bytes in 45ms
06-27 08:24:47.136: I/dalvikvm(15475): threadid=15: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI
06-27 08:24:47.136: I/dalvikvm(15475):   method requires 28+20+20=68 bytes, fp is 0x45209338 (56 left)
06-27 08:24:47.140: I/dalvikvm(15475):   expanding stack end (0x45209300 to 0x45209000)
06-27 08:24:47.140: I/dalvikvm(15475): Shrank stack (to 0x45209300, curFrame is 0x4520937c)
06-27 08:24:47.159: I/dalvikvm(15475): threadid=16: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI
06-27 08:24:47.159: I/dalvikvm(15475):   method requires 28+20+20=68 bytes, fp is 0x4520c338 (56 left)
06-27 08:24:47.167: I/dalvikvm(15475):   expanding stack end (0x4520c300 to 0x4520c000)
06-27 08:24:47.167: I/dalvikvm(15475): Shrank stack (to 0x4520c300, curFrame is 0x4520c37c)
06-27 08:24:47.175: I/dalvikvm(15475): threadid=17: stack overflow on call to Ljava/lang/AbstractStringBuilder;.enlargeBuffer:VI
06-27 08:24:47.175: I/dalvikvm(15475):   method requires 28+20+20=68 bytes, fp is 0x4520f338 (56 left)
06-27 08:24:47.175: I/dalvikvm(15475):   expanding stack end (0x4520f300 to 0x4520f000)
06-27 08:24:47.175: I/dalvikvm(15475): Shrank stack (to 0x4520f300, curFrame is 0x4520f37c)

这当然是更有用的信息,但现在我正在努力解决以下问题:

  • 尽管调用了 subclass.uncaughtException(),应用程序现在不会强制关闭。为什么?
  • 所有这些堆栈溢出的含义是什么?我能做些什么让我那可怜的 Android 测试设备如此费力?
  • 如何判断我的代码中的哪一部分导致了此问题?

更新 Log.getStackTraceString(paramThrowable); 实际上并没有打印任何东西。我收到的额外打印品来自 bogus subclass.uncaughtException(paramThread, paramThrowable);记录完整堆栈跟踪的正确方法是使用 Log.e(TAG, "uncaughtException", throwable) .

现在剩下的唯一问题是如何重新抛出异常?只是做一个throw paramThrowable

回答我的最后一个问题:Eclipse 不会让我在没有包围 try/catch 的情况下抛出,这让我明白我想要的不是重新抛出而是 killProcess()。问题解决了。

最佳答案

您可以在应用程序的开头设置一个默认的未捕获异常处理程序,并在其中记录一些数据(下面的示例使用的是 java 记录器,但很容易转换为 Android):

private static void setDefaultUncaughtExceptionHandler() {
    try {
        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                logger.error("Uncaught Exception detected in thread {}", t, e);
            }
        });
    } catch (SecurityException e) {
        logger.error("Could not set the Default Uncaught Exception Handler", e);
    }
}

关于java - 线程退出并出现未捕获的异常 : NO stack trace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11225209/

相关文章:

java - 如何向有效负载表达式添加两个路径变量?

java - 拦截方法调用并添加/丰富参数

java - java url 和命名空间转换

android - 来自 AlertDialog 的 findViewById(使用自定义布局)- NullPointerException

android - 如何在android电子邮件中插入一个URL

java - Character.getNumericValue 的反面是什么

android - 打开电子邮件中的链接以在应用程序而不是网站中进行确认

python - 我的快速排序因大小为 2^13 的列表而挂起

Java错误: "Exception in thread "main"java. lang.StackOverflowError“构造函数中的问题

c++ - 在 Visual Studio 中监视调用堆栈大小