How can I convert a stack trace to a string?

前端 未结 30 1170
再見小時候
再見小時候 2020-11-22 14:51

What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace?

相关标签:
30条回答
  • 2020-11-22 15:05

    If you don't want to use an external library and you're not developing for Android, you could create an 'extension' method like this:

    public static String getStackTraceString(Throwable e) {
        return getStackTraceString(e, "");
    }
    
    private static String getStackTraceString(Throwable e, String indent) {
        StringBuilder sb = new StringBuilder();
        sb.append(e.toString());
        sb.append("\n");
    
        StackTraceElement[] stack = e.getStackTrace();
        if (stack != null) {
            for (StackTraceElement stackTraceElement : stack) {
                sb.append(indent);
                sb.append("\tat ");
                sb.append(stackTraceElement.toString());
                sb.append("\n");
            }
        }
    
        Throwable[] suppressedExceptions = e.getSuppressed();
        // Print suppressed exceptions indented one level deeper.
        if (suppressedExceptions != null) {
            for (Throwable throwable : suppressedExceptions) {
                sb.append(indent);
                sb.append("\tSuppressed: ");
                sb.append(getStackTraceString(throwable, indent + "\t"));
            }
        }
    
        Throwable cause = e.getCause();
        if (cause != null) {
            sb.append(indent);
            sb.append("Caused by: ");
            sb.append(getStackTraceString(cause, indent));
        }
    
        return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-22 15:06

    WARNING: Does not include cause (which is usually the useful bit!)

    public String stackTraceToString(Throwable e) {
        StringBuilder sb = new StringBuilder();
        for (StackTraceElement element : e.getStackTrace()) {
            sb.append(element.toString());
            sb.append("\n");
        }
        return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-22 15:06

    Kotlin

    Extending the Throwable class will give you the String property error.stackTraceString:

    val Throwable.stackTraceString: String
      get() {
        val sw = StringWriter()
        val pw = PrintWriter(sw)
        this.printStackTrace(pw)
        return sw.toString()
      }
    
    0 讨论(0)
  • 2020-11-22 15:08

    I wonder why no one mentioned ExceptionUtils.getStackFrames(exception)

    For me it's the most convenient way to dump stacktrace with all its causes to the end:

    String.join("\n", ExceptionUtils.getStackFrames(exception));
    
    0 讨论(0)
  • 2020-11-22 15:11

    Guava's Throwables class

    If you have the actual Throwable instance, Google Guava provides Throwables.getStackTraceAsString().

    Example:

    String s = Throwables.getStackTraceAsString ( myException ) ;
    
    0 讨论(0)
  • 2020-11-22 15:11

    Here is a version that is copy-pastable directly into code:

    import java.io.StringWriter; 
    import java.io.PrintWriter;
    
    //Two lines of code to get the exception into a StringWriter
    StringWriter sw = new StringWriter();
    new Throwable().printStackTrace(new PrintWriter(sw));
    
    //And to actually print it
    logger.info("Current stack trace is:\n" + sw.toString());
    

    Or, in a catch block

    } catch (Throwable t) {
        StringWriter sw = new StringWriter();
        t.printStackTrace(new PrintWriter(sw));
        logger.info("Current stack trace is:\n" + sw.toString());
    }
    
    0 讨论(0)
提交回复
热议问题