Java Throwable to String stackTrace(Throwable e)

Here you can find the source of stackTrace(Throwable e)

Description

Returns the output of printStackTrace as a String.

License

Open Source License

Parameter

Parameter Description
e A Throwable.

Return

A String.

Declaration

public static final String stackTrace(Throwable e) 

Method Source Code


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

public class Main {
    /**/*from  w  ww  . j  a  va 2 s  . com*/
     * Returns the output of printStackTrace as a String.
     *
     * @param e A Throwable.
     * @return A String.
     */
    public static final String stackTrace(Throwable e) {
        String throwableAsString = null;
        try {
            // And show the Error Screen.
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(buf, true));
            throwableAsString = buf.toString();
        } catch (Exception f) {
            // Do nothing.
        }
        return throwableAsString;
    }

    /**
     * Returns the output of printStackTrace as a String.
     *
     * @param e A Throwable.
     * @param addPre a boolean to add HTML <pre> tags around the stacktrace
     * @return A String.
     */
    public static final String stackTrace(Throwable e, boolean addPre) {
        if (addPre) {
            return "<pre>" + stackTrace(e) + "</pre>";
        } else {
            return stackTrace(e);
        }
    }
}

Related

  1. stacktrace(Logger logger, Throwable ex)
  2. stackTrace(Throwable cause)
  3. stackTrace(Throwable e)
  4. stackTrace(Throwable e)
  5. stackTrace(Throwable e)
  6. stackTrace(Throwable exception)
  7. stackTrace(Throwable t)
  8. stackTrace(Throwable t)
  9. stackTrace(Throwable t)