Example usage for java.lang Throwable printStackTrace

List of usage examples for java.lang Throwable printStackTrace

Introduction

In this page you can find the example usage for java.lang Throwable printStackTrace.

Prototype

public void printStackTrace(PrintWriter s) 

Source Link

Document

Prints this throwable and its backtrace to the specified print writer.

Usage

From source file:Main.java

public static String dumpException(Throwable e) {
    StringWriter sw = new StringWriter(160);
    sw.write(e.getClass().getName());/*from  w  w w. j a va 2s  .c  o  m*/
    sw.write(":\n");
    e.printStackTrace(new PrintWriter(sw));
    return sw.toString();
}

From source file:com.tera.common.util.ExceptionUtil.java

/**
 * Simplified version of getting stacktracer from {@link Throwable} For
 * advanced use - see {@link org.apache.commons.lang.exception.ExceptionUtils}
 * /* ww w .ja va  2  s  .co m*/
 * @param throwable
 * @return
 */
public static String getStackTrace(Throwable throwable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    throwable.printStackTrace(printWriter);
    return result.toString();
}

From source file:Main.java

/**
 * Extract the full stacktrace from {@code t} into a String for logging
 * @param t an exception/*from   ww  w . ja va2  s  .co m*/
 * @return a string representation of the full stack trace
 */
public static String sprintFullStack(final Throwable t) {
    if (t == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    t.printStackTrace(new PrintWriter(writer));
    return writer.toString();
}

From source file:Main.java

public static String getStackTrace(Throwable t) {
    StringWriter stringWritter = new StringWriter();
    PrintWriter printWritter = new PrintWriter(stringWritter, true);
    t.printStackTrace(printWritter);
    printWritter.flush();/*from  www. j av a  2s.c o m*/
    stringWritter.flush();
    return stringWritter.toString();
}

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);
    try {//  ww w  . java2s.c o m
        result = bos.toString("utf-8");
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String getStackTraceString(Throwable t) {
    String value;//from  www.  jav  a2s.  c o  m
    if (t == null) {
        value = "";
    } else {
        final StringWriter stackTrace = new StringWriter();
        t.printStackTrace(new PrintWriter(stackTrace));
        value = stackTrace.toString();
    }
    return value;
}

From source file:Main.java

/**
 * <p>/*  w ww  .j  a  v a  2 s .com*/
 * Print the full stack trace of a <code>Throwable</code> object into a
 * string buffer and return the corresponding string.  
 * </p>
 */
public static String printStackTrace(Throwable t) {
    java.io.StringWriter strWriter = new java.io.StringWriter();
    java.io.PrintWriter prWriter = new java.io.PrintWriter(strWriter);
    t.printStackTrace(prWriter);
    prWriter.flush();
    return strWriter.toString();
}

From source file:Main.java

public static String getExceptionCauseString(final Throwable ex) {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final PrintStream ps = new PrintStream(bos);

    try {// w  w  w.  j a v  a 2s . c o  m
        // print directly
        Throwable t = ex;
        while (t.getCause() != null) {
            t = t.getCause();
        }
        t.printStackTrace(ps);
        return toVisualString(bos.toString());
    } finally {
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

private static String getStringFromException(Throwable e) {
    String result = "";
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps);
    try {// w w w. j  a va 2 s . c  o m
        result = bos.toString("UTF-8");
    } catch (UnsupportedEncodingException e1) {
        // won't happen
    }
    return result;
}

From source file:Main.java

public static String getErrorReport(String message, Throwable e) {
    if (message != null && message.length() > 0) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return String.format(ERROR_REPORT, message, byteArrayOutputStream.toString());
    } else {//from   w  w w  .j a  v  a2s . c  o m
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(byteArrayOutputStream));
        return byteArrayOutputStream.toString();
    }

}