Java Throwable to String stackTraceToString(Throwable t)

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

Description

Gets a String containing the stack trace for the specified Throwable.

License

Open Source License

Parameter

Parameter Description
t the Throwable to get the stack trace for

Return

the stack trace as a String

Declaration

public static final String stackTraceToString(Throwable t) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.io.PrintStream;
import java.io.IOException;
import java.io.Reader;

public class Main {
    /**/*from   www .j  av  a 2 s  . c o m*/
     * Gets a String containing the stack trace for the specified Throwable.
     *
     * @param t the Throwable to get the stack trace for
     * @return the stack trace as a String
     */
    public static final String stackTraceToString(Throwable t) {

        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(buf);
        t.printStackTrace(out);
        out.flush();
        return (buf.toString());
    }

    /**
     * <p>Reads characters from a Reader and creates a String
     * from those characters.</p>
     *
     * NOTE: This method will terminate the String if a null character
     * is encountered.
     *
     * @param reader  the Reader to read characters from
     * @return the built up String
     * @throws IOException on any error
     */
    public static String toString(Reader reader) throws IOException {

        StringBuffer buf = new StringBuffer(1024);
        while (true) {

            int c = reader.read();
            if (c <= 0) {
                break;
            }

            buf.append(c);
        }

        return (buf.toString());
    }
}

Related

  1. StackTraceToString(Throwable ex)
  2. stacktraceToString(Throwable t)
  3. stackTraceToString(Throwable t)
  4. stackTraceToString(Throwable t)
  5. stackTraceToString(Throwable t)
  6. stackTraceToString(Throwable th)
  7. stackTraceToString(Throwable throwable)