简体   繁体   中英

how to get a complete stack trace in android?

I am using the following code

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

And it returns the stack trace before "Caused by : " but i want to get after that "Caused by : " also. Thanks in advance

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)

I can't able to get that "Caused By : " lines

If your Throwable has a cause, it can be retrieved by calling

Throwable cause = aThrowable.getCause();

Then you can recursivly call

getCombinedStackTrace(cause);

and concatinate the results to get the complete stack-trace.

You can use this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM