Java Stacktrace Print printStackTraceUsingStream(Throwable throwable)

Here you can find the source of printStackTraceUsingStream(Throwable throwable)

Description

print Stack Trace Using Stream

License

Apache License

Declaration

public static String printStackTraceUsingStream(Throwable throwable) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Main {
    public static String printStackTraceUsingStream(Throwable throwable) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        throwable.printStackTrace(ps);/*from   ww w.ja v a2s.c o m*/
        return baos.toString();
    }

    public static String printStackTrace(Throwable throwable) {
        StringBuilder sb = new StringBuilder();
        sb.append("Caused by: ").append(throwable).append('\n');
        for (StackTraceElement ste : throwable.getStackTrace()) {
            sb.append('\t').append(ste.toString()).append('\n');
        }
        if (throwable.getCause() != null) {
            return sb.toString() + printStackTrace(throwable.getCause());
        } else {
            return sb.toString();
        }
    }
}

Related

  1. printStackTraceHTML(Throwable e)
  2. printStackTraces()
  3. printStackTraceToOutputStream(Throwable t, OutputStream out)
  4. printStackTraceToString(final Throwable t)
  5. printStackTraceToString(Throwable aThrowable)