Java Throwable to String getStackTrace(final Throwable t)

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

Description

get Stack Trace

License

Open Source License

Declaration

public static String getStackTrace(final Throwable t) 

Method Source Code

//package com.java2s;
/*/*w  ww.j a va  2 s . co  m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

import java.io.ByteArrayOutputStream;

import java.io.PrintWriter;

public class Main {
    public static String getStackTrace(final Throwable t) {
        final ByteArrayOutputStream bas = new ByteArrayOutputStream();
        final PrintWriter pw = new PrintWriter(bas);
        t.printStackTrace(pw);
        pw.close();
        return bas.toString();
    }

    /**
     * Return a stringified version of the array.
     * 
     * @param array the array
     * @param delim the delimiter to use between array components
     * @return the string form of the array
     */
    public static String toString(final Object[] array, final String delim) {
        if (array == null) {
            return ""; //$NON-NLS-1$
        }
        if (array.length == 0) {
            return "[]"; //$NON-NLS-1$
        }
        final StringBuffer sb = new StringBuffer();
        sb.append('[');
        for (int i = 0; i < array.length; ++i) {
            if (i != 0) {
                sb.append(delim);
            }
            sb.append(array[i]);
        }
        sb.append(']');
        return sb.toString();
    }

    /**
     * Return a stringified version of the array, using a ',' as a delimiter
     * 
     * @param array the array
     * @return the string form of the array
     * @see #toString(Object[], String)
     */
    public static String toString(final Object[] array) {
        return toString(array, ","); //$NON-NLS-1$
    }
}

Related

  1. getStackTrace(final Throwable aThrowable)
  2. getStackTrace(final Throwable e)
  3. getStackTrace(final Throwable error)
  4. getStackTrace(final Throwable exception)
  5. getStackTrace(final Throwable t)
  6. getStackTrace(final Throwable t)
  7. getStackTrace(final Throwable t)
  8. getStackTrace(final Throwable throwable)
  9. getStackTrace(Throwable aThrowable)