Java Throwable to String getStackTrace(Throwable t)

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

Description

Returns a stack trace as a string.

License

Open Source License

Parameter

Parameter Description
t a parameter

Declaration

public static String getStackTrace(Throwable t) 

Method Source Code


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

public class Main {
    /**//from  w  w w  . j  a  va2  s.c  om
     * Use for inserting new lines in formatted string representations. This is an application-wide
     * constant.
     */
    public static final String NEWLINE = System.getProperty("line.separator");

    /**
     * Returns a stack trace as a string.
     * 
     * @param t
     * @return
     */
    public static String getStackTrace(Throwable t) {
        StringWriter out = new StringWriter();
        PrintWriter writer = new PrintWriter(out);

        t.printStackTrace(writer);
        return out.toString();
    }

    /**
     * Enhanced toString representation of complex objects such as arrays and throwables.
     * 
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        if (obj instanceof Throwable) {
            return toString((Throwable) obj, Integer.MAX_VALUE);
        } else if (obj instanceof Object[]) {
            Object[] arr = (Object[]) obj;
            StringBuilder sb = new StringBuilder();
            sb.append("Array[").append(arr.length).append("]");
            for (int i = 0; i < arr.length; i++) {
                sb.append(' ').append(arr[i]);
            }
            return sb.toString();
        } else if (obj != null) {
            return obj.toString();
        }

        return null;
    }

    /**
     * Prints a formatted message containing all chained exception messages.
     * 
     * @param t
     * @param ctx
     * @return
     */
    public static String toString(Throwable t, int stackTraceSize) {
        if (stackTraceSize == -1) {
            return getStackTrace(t);
        }

        StringBuilder sb = new StringBuilder();

        int level = -1;
        Throwable cause = t;
        while (cause != null) {
            level++;
            if (cause != t)
                sb.append(NEWLINE).append(fill(' ', level << 1)).append("Caused by: ");

            sb.append(cause.getClass().getName()).append(": ").append(cause.getMessage());

            if (stackTraceSize != -1) {
                StackTraceElement[] ste = cause.getStackTrace();
                int size = Math.min(stackTraceSize, ste.length);
                for (int i = 0; i < size; i++) {
                    sb.append(NEWLINE).append(fill(' ', (level + 1) << 1)).append("- ");
                    sb.append(ste[i].getClassName()).append(':');
                    sb.append(ste[i].getMethodName()).append(':');
                    sb.append(ste[i].getLineNumber());
                    sb.append('(').append(ste[i].getFileName()).append(')');
                }
            }

            if (cause != cause.getCause()) {
                cause = cause.getCause();
            } else {
                cause = null;
            }
        }

        return sb.toString();
    }

    /**
     * Create a string by repeating the source string the specified number of
     * times.
     * 
     * @param source - source string to be repeated
     * @param count - the number of times to repeat the source string
     * @return String
     */
    public static String fill(char fchar, int count) {
        StringBuilder sb = new StringBuilder(count);

        for (int i = 0; i < count; i++) {
            sb.insert(i, fchar);
        }

        return sb.toString();
    }
}

Related

  1. GetStacktrace(Throwable t)
  2. getStackTrace(Throwable t)
  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)