Java Stacktrace Print printStackTrace()

Here you can find the source of printStackTrace()

Description

Prints this throwable and its backtrace to the specified print stream.

License

Apache License

Parameter

Parameter Description
s <code>PrintStream</code> to use for output

Declaration

public static void printStackTrace() 

Method Source Code


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

import java.io.*;

public class Main {
    /**/*from  ww w  . j  a v  a2 s .  c  o m*/
     * Prints this throwable and its backtrace to the specified print stream.
     *
     * @param s <code>PrintStream</code> to use for output
     */
    public static void printStackTrace() {
        printStackTrace(System.err);
    }

    /**
     * Prints this throwable and its backtrace to the specified print stream.
     *
     * @param s <code>PrintStream</code> to use for output
     */
    public static void printStackTrace(PrintStream s) {
        StackTraceElement[] trace = getStackTrace(2);
        synchronized (s) {
            for (int i = 0; i < trace.length; i++)
                s.println("\tat " + trace[i]);
        }
    }

    /**
     * Prints this throwable and its backtrace to the specified print stream.
     *
     * @param s <code>PrintStream</code> to use for output
     */
    public static void printStackTrace(StackTraceElement[] trace, PrintStream s) {
        synchronized (s) {
            for (int i = 0; i < trace.length; i++)
                s.println("\tat " + trace[i]);
        }
    }

    /**
     * Prints this throwable and its backtrace to the specified print stream.
     *
     * @param s <code>PrintStream</code> to use for output
     */
    public static void printStackTrace(StackTraceElement[] trace) {
        printStackTrace(trace, System.err);
    }

    /**
     * return the current stack trace
     *
     * @return non-null array of StackTraceElement
     */
    public static StackTraceElement[] getStackTrace() {
        StackTraceElement[] ret = getStackTrace(1);
        return ret;
    }

    /**
     * return the current stack trace
     *
     * @return non-null array of StackTraceElement
     */
    public static StackTraceElement[] getStackTrace(int drop) {
        Exception temp = new Exception();
        StackTraceElement[] all = temp.getStackTrace();
        int ndropped = 1 + drop;
        StackTraceElement[] ret = new StackTraceElement[all.length - ndropped];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = all[i + ndropped];
        }
        // printStackTrace(ret);
        return ret;
    }
}

Related

  1. printStackTrace()
  2. printStackTrace()
  3. printStackTrace()
  4. printStackTrace()
  5. printStackTrace()
  6. printStackTrace()
  7. printStackTrace()
  8. printStackTrace(Exception e)
  9. printStackTrace(Exception e)