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 :」の前にスタックトレースを返しますが、その後「Caused by :」も取得したいです。前もって感謝します

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 に答える