Java Utililty Methods Stacktrace Print

List of utility methods to do Stacktrace Print

Description

The list of methods to do Stacktrace Print are organized into topic(s).

Method

StringformatStackTrace(Thread thread)
format Stack Trace
Throwable t = new Throwable(
        String.format("Stack trace for thread %s (State: %s):", thread.getName(), thread.getState()));
t.setStackTrace(thread.getStackTrace());
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString();
StringformatStackTrace(Throwable t)
format Stack Trace
StringWriter sw = new StringWriter();
try {
    PrintWriter p = new PrintWriter(sw);
    t.printStackTrace(p);
} catch (Exception e) {
return sw.toString();
StringformatStackTrace(Throwable t)
Formats a throwable's stack trace
if (t == null)
    return "";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.printStackTrace(new PrintStream(baos, true));
try {
    baos.flush();
} catch (IOException e) { 
return baos.toString();
StringformatStackTrace(Throwable t, String prefix)
Formats the stack trace and returns the result.
return formatStackTrace(null, t, prefix).toString();
StringformatStackTraceToString(Exception ex)
format Stack Trace To String
if (ex == null) {
    throw new IllegalArgumentException("ex can't be null.");
String result = null;
StringWriter stringWriter = null;
PrintWriter printWriter = null;
try {
    stringWriter = new StringWriter();
...
voidprintStackTrace()
print Stack Trace
for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.print(ste);
voidprintStackTrace()
print Stack Trace
try {
    throw new RuntimeException("Debugging purposes only");
} catch (Exception ex) {
    ex.printStackTrace();
voidprintStackTrace()
Prints the stack trace.
final Throwable t = new Throwable();
final StackTraceElement trace[] = t.getStackTrace();
for (final StackTraceElement element : trace) {
    System.err.println(element.getClassName() + "." + element.getMethodName());
voidprintStackTrace()
print Stack Trace
System.out.println("CURRENT STACK TRACE:");
for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
    System.out.println(s.getClassName() + "." + s.getMethodName() + " (line " + s.getLineNumber() + ") "
            + (s.isNativeMethod() ? "NATIVE" : ""));
System.out.println("END OF STACK TRACE");
voidprintStackTrace()
print Stack Trace
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
int cnt = 2;
for (StackTraceElement element : stackTraceElements) {
    if (cnt-- <= 0)
        System.out.println("  " + element.getClassName() + "." + element.getMethodName() + " ("
                + element.getFileName() + ":" + element.getLineNumber() + ")");