Java Stacktrace Print printStackTrace(PrintStream ps, Exception e)

Here you can find the source of printStackTrace(PrintStream ps, Exception e)

Description

print Stack Trace

License

Open Source License

Declaration

public static void printStackTrace(PrintStream ps, Exception e) 

Method Source Code


//package com.java2s;

import java.io.*;

public class Main {
    public static void printStackTrace(PrintStream ps, Exception e) {
        if (e == null)
            print(ps, getStackTrace());/*from w  w w.ja v a  2  s  . com*/
        else
            print(ps, stackTraceToString(e.getStackTrace()));
    }

    public static void printStackTrace(PrintStream ps) {
        printStackTrace(ps, null);
    }

    public static void printStackTrace() {
        System.out.print(getStackTrace());
    }

    public static void print(PrintStream ps, String buf) {
        System.out.print(buf);
        if (ps != null) {
            ps.print(buf);
        }
    }

    public static String getStackTrace() {
        Thread t = Thread.currentThread();
        // FMStackTrace fst = new FMStackTrace(t.getStackTrace());
        // return fst.toString();
        StackTraceElement[] ste = t.getStackTrace();
        return stackTraceToString(ste);
    }

    public static String stackTraceToString(StackTraceElement[] ste) {
        String str = "";
        for (int i = 0; i < ste.length; i++) {
            str += String.format("    [%02d] %s \n", i, ste[i].toString());
        }
        return str;
    }
}

Related

  1. printStackTrace(PrintStream out)
  2. printStackTrace(PrintStream out)
  3. printStackTrace(PrintStream out, StackTraceElement[] stackTrace)
  4. printStackTrace(PrintStream printStream, Thread thread, Throwable throwable)
  5. printStackTrace(PrintStream ps)
  6. printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace)
  7. printStackTrace(StackTraceElement[] elements)
  8. printStackTrace(StackTraceElement[] elements)
  9. printStackTrace(StackTraceElement[] stackTrace)