Java Stacktrace Print printStackTrace(PrintStream printStream, Thread thread, Throwable throwable)

Here you can find the source of printStackTrace(PrintStream printStream, Thread thread, Throwable throwable)

Description

print Stack Trace

License

Open Source License

Declaration

public static void printStackTrace(PrintStream printStream, Thread thread, Throwable throwable) 

Method Source Code


//package com.java2s;
/*/*from  w  ww  .  j a va2s .c  o m*/
 * Copyright (C) 2016 Yadieet SA <yadieet@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.PrintStream;

public class Main {
    public static void printStackTrace(PrintStream printStream, Thread thread, Throwable throwable) {
        StringBuilder stringBuilder = new StringBuilder(1500);
        stringBuilder.append('[').append(thread.getName()).append(" thread] exception : ").append(throwable)
                .append(System.lineSeparator());
        StackTraceElement[] stackTraceElements = throwable.getStackTrace();
        for (StackTraceElement element : stackTraceElements)
            stringBuilder.append("    at ").append(element).append(System.lineSeparator());

        Throwable cause;
        while ((cause = throwable.getCause()) != null) {
            stringBuilder.append("Caused by: ").append(cause).append(System.lineSeparator());
            stackTraceElements = cause.getStackTrace();
            for (StackTraceElement element : stackTraceElements)
                stringBuilder.append("    at ").append(element).append(System.lineSeparator());

            throwable = cause;
        }

        printStream.println(stringBuilder);
    }

    public static void printStackTrace(Throwable throwable) {
        printStackTrace(System.err, Thread.currentThread(), throwable);
    }

    public static void printStackTrace(Thread thread, Throwable throwable) {
        printStackTrace(System.err, thread, throwable);
    }
}

Related

  1. printStackTrace(IOException ioe)
  2. PrintStackTrace(PrintStream out)
  3. printStackTrace(PrintStream out)
  4. printStackTrace(PrintStream out)
  5. printStackTrace(PrintStream out, StackTraceElement[] stackTrace)
  6. printStackTrace(PrintStream ps)
  7. printStackTrace(PrintStream ps, Exception e)
  8. printStackTrace(PrintWriter writer, Thread thread, StackTraceElement[] trace)
  9. printStackTrace(StackTraceElement[] elements)