0

我正在使用以下代码

public static String getCombinedStackTrace(Throwable aThrowable) {

            final StringBuilder result = new StringBuilder();
            result.append(aThrowable.toString());
            result.append(',');

            String oneElement;

            for (StackTraceElement element : aThrowable.getStackTrace() ) {
                oneElement = element.toString();
                result.append( oneElement );
                result.append( ",");
            }
return result.toString();
}

它返回“引起:”之前的堆栈跟踪,但我也想在“引起:”之后得到。提前致谢

Caused by: java.sql.BatchUpdateException: failed batch
    at org.hsqldb.jdbc.jdbcStatement.executeBatch(jdbcStatement.java:1102)
    at org.hsqldb.jdbc.jdbcPreparedStatement.executeBatch(jdbcPreparedStatement.java:514)
    at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)

我无法获得“Caused By:”行

4

2 回答 2

0

如果您的 Throwable 有原因,可以通过调用来检索它

Throwable cause = aThrowable.getCause();

然后你可以递归调用

getCombinedStackTrace(cause);

并连接结果以获得完整的堆栈跟踪。

于 2013-06-07T12:57:01.117 回答
0

您可以使用以下代码:

import java.io.PrintWriter;
import java.io.StringWriter;

 Writer result = new StringWriter();
            PrintWriter printWriter = new PrintWriter(result);
            e.printStackTrace(printWriter);
            //String stacktrace = result.toString();
            //Report += stacktrace;
            printWriter.append( "\n"
                    + "Cause : \n"
                    + "======= \n");

            // If the exception was thrown in a background thread inside
            // AsyncTask, then the actual exception can be found with getCause
            Throwable cause = e.getCause();
            while (cause != null) {
                cause.printStackTrace(printWriter);
                cause = cause.getCause();
            }

            String trace = result.toString(); // here is the result!
            printWriter.close();
于 2013-06-07T13:04:11.843 回答