Java Throwable to String getStackTrace(Throwable t)

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

Description

get Stack Trace

License

Open Source License

Declaration

public static String getStackTrace(Throwable t) 

Method Source Code

//package com.java2s;
import java.io.PrintWriter;
import java.io.StringWriter;

public class Main {
    public static String getStackTrace(Throwable t) {
        if (t == null) {
            try {
                throw new Exception();
            } catch (Exception e) {
                t = e;/*  w  w w  . j a v  a  2  s .c om*/
            }
        }
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        sw.flush();
        return sw.toString();
    }

    public static String toString(Object[] arr) {
        return toString(arr, true);
    }

    public static String toString(Object[] arr, boolean square) {
        if (arr == null) {
            return "null";
        }
        StringBuffer sb = new StringBuffer();
        if (square) {
            sb.append("[");
        } else {
            sb.append("(");
        }
        for (int i = 0; i < arr.length; ++i) {// Object o : arr ) {
            if (i > 0) {
                sb.append(",");
            }
            if (arr[i] == null) {
                sb.append("null");
            } else {
                sb.append(arr[i].toString());
            }
        }
        if (square) {
            sb.append("]");
        } else {
            sb.append(")");
        }

        return sb.toString();
    }
}

Related

  1. getStackTrace(Throwable pException)
  2. getStackTrace(Throwable pThrowable_)
  3. getStackTrace(Throwable t)
  4. getStackTrace(Throwable t)
  5. getStackTrace(Throwable t)
  6. getStackTrace(Throwable t)
  7. getStackTrace(Throwable t)
  8. getStackTrace(Throwable t)
  9. getStackTrace(Throwable t)