从模糊堆栈跟踪中寻找异常原因

时间:2013-03-27 19:01:08

标签: java android exception exception-handling

我有一个Android应用程序,最近神秘地强行关闭使用4.x设备的一些用户。看看异常堆栈,似乎错误发生在我的任何代码运行之前,但我认为我正在做一些导致这种情况的事情。

* 我如何找到原因或堆叠有问题?否则,如果这是一个完整的堆栈跟踪,那么在我的任何代码运行之前,Android真的可能会失败吗? *

我发现后者不太可能,因为我在UncaughtExceptionHandler的{​​{1}}方法中注册了Application,而且我通常会从用户那里获取一个文件。

我的错误记录代码如下:

我使用应用程序实现扩展onCreate()。发生错误时,我将异常信息写入Thread.UncaughtExceptionHandler中的日志。这就是我找到要打印的字符串的方式:

uncaughtException(Thread thread,Throwable ex)

将其输出到文件:

// Get exception info String newLine = "\r\n"; String message = "Message: " + ex.getMessage(); String cause = "Cause: " + ex.getCause(); StackTraceElement[] stes = ex.getStackTrace(); String stack; // build stack trace stack = "Stack: " + newLine; for ( int i = 0; i < stes.length; i++ ) { stack += stes[i].toString(); stack += newLine; } // print out message // print out cause // print out stack

P.S。 是的,我知道ACRA,但我现在不想使用它。

2 个答案:

答案 0 :(得分:1)

也许你的应用程序导致了一个被捕获然后重新抛出的异常。

您只打印最后一个例外的Stacktrace。也许此异常有原因(ex.getCause()不是null)。在这种情况下,您还应该打印原因的堆栈跟踪(可能是原因的原因..)。

您不必自己动手。 Throwable有一个很好的方法可以将完整的堆栈跟踪和回溯打印到PrintStream

public void printStackTrace(PrintStream s)

之后,您只需将printStream写入日志。

答案 1 :(得分:1)

@micha仍然是公认的答案,但我想我会根据他的答案显示我修改过的解决方案。

      @Override
public void uncaughtException(Thread thread, Throwable ex)
    {
    // This needs to be AIR TIGHT!. Do everything in a try-catch to prevent
    // any exceptions from EVER occurring here. If an error does occur here 
    // then we will hang
    // indefinitely and get an ANR.
    try
        {
        FileManager fileMgr = FileManager.getInstance( myApp );
        String logName = "ErrorLog.log";

        // print exception to byte array

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        PrintStream printer = new PrintStream( out );
        ex.printStackTrace( printer );

        // write out byte array as string to log file.

        fileMgr.writeToLogFile( logName, out.toString(), false );
        }
    catch ( Exception e )
        {
        }

    // This allows the system to handle to exception and close the App.

    defaultHandler.uncaughtException( thread, ex );
    }

我的自定义FileManager类会对文件进行实际写入,但我使用Throwable的{​​{1}}来获取完整的堆栈跟踪,这比我本可以做的要深得多靠我自己。

相关问题