Java Stacktrace Print printStackTrace(PrintStream ps)

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

Description

print Stack Trace

License

Open Source License

Declaration

public static void printStackTrace(PrintStream ps) 

Method Source Code


//package com.java2s;
/*//from  w  ww.ja  v a2 s  . com
 * Copyright (C) 2005-2007 Oleh Hapon ohapon@users.sourceforge.net
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 * Oleh Hapon
 * Kyiv, UKRAINE
 * ohapon@users.sourceforge.net
 */

import java.io.PrintStream;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.IOException;

public class Main {
    public static void printStackTrace(PrintStream ps) {
        if (ps == null) {
            throw new IllegalArgumentException("PrintStream is null");
        }
        try {
            throw new Exception();
        } catch (Exception ex) {
            ps.println(getStackTrace(ex));
        }
    }

    public static String getStackTrace(Throwable th) {
        if (th == null) {
            throw new IllegalArgumentException("Throwable is null");
        }

        StringWriter sw = new StringWriter();
        try {
            PrintWriter pw = new PrintWriter(sw);
            try {
                th.printStackTrace(pw);
                return sw.toString();
            } finally {
                pw.close();
            }
        } finally {
            try {
                sw.close();
            } catch (IOException ignore) {
                //
            }
        }
    }
}

Related

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