Java Throwable to String getStackTrace(Throwable th)

Here you can find the source of getStackTrace(Throwable th)

Description

get Stack Trace

License

Open Source License

Declaration

public static String getStackTrace(Throwable th) 

Method Source Code


//package com.java2s;
/*/*w  ww .j a  v  a2s. co  m*/
 * 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 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) {
                //
            }
        }
    }

    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));
        }
    }
}

Related

  1. getStackTrace(Throwable t)
  2. getStackTrace(Throwable t)
  3. getStackTrace(Throwable t)
  4. getStackTrace(Throwable t0)
  5. getStackTrace(Throwable tException)
  6. getStackTrace(Throwable throwable)
  7. getStackTrace(Throwable throwable)
  8. getStackTrace(Throwable throwable)
  9. getStackTrace(Throwable throwable)