Java Utililty Methods Throwable to String

List of utility methods to do Throwable to String

Description

The list of methods to do Throwable to String are organized into topic(s).

Method

StringstackTraceToString(Throwable e)
Gets an exception's stack trace as a String
if (e == null) {
    return "";
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(stream);
e.printStackTrace(writer);
writer.flush();
return stream.toString();
...
StringstackTraceToString(Throwable e)
stack Trace To String
String retValue = null;
StringWriter sw = null;
PrintWriter pw = null;
try {
    sw = new StringWriter();
    pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    retValue = sw.toString();
...
StringstackTraceToString(Throwable eIn)
This is a convenience function for printing the content of a stack track to a String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
for (int i = 0; i < MAX_NUM_STACK_TRACE_ENTRIES; i++) {
    StackTraceElement currElem = eIn.getStackTrace()[i];
    pw.println(currElem.toString());
return sw.toString();
StringstackTraceToString(Throwable ex)
Converts a nested exception into a nicer message
StringBuffer traceBuf = new StringBuffer();
traceBuf.append("Util.1"); 
if (ex != null) {
    traceBuf.append(ex.getClass().getName());
    String message = ex.getMessage();
    if (message != null) {
        traceBuf.append("Util.2"); 
        traceBuf.append(message);
...
StringStackTraceToString(Throwable ex)
Stack Trace To String
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
PrintStream printStream = new PrintStream(byteStream);
ex.printStackTrace(printStream);
printStream.flush();
String stackTrace = byteStream.toString();
printStream.close();
return stackTrace;
StringstacktraceToString(Throwable t)
stacktrace To String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringstackTraceToString(Throwable t)
stack Trace To String
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
return sw.toString();
StringstackTraceToString(Throwable t)
stack Trace To String
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.getBuffer().toString();
StringstackTraceToString(Throwable t)
Print exception stack trace to string if allowed.
if (PRINT_STACK_TRACE) {
    StringWriter descr = new StringWriter() {
        boolean writeOK = true;
        public void write(char[] cbuf, int off, int len) {
            if (canWrite())
                super.write(cbuf, off, len);
        public void write(int c) {
...
StringstackTraceToString(Throwable t)
Gets a String containing the stack trace for the specified Throwable.
ByteArrayOutputStream buf = new ByteArrayOutputStream();
PrintStream out = new PrintStream(buf);
t.printStackTrace(out);
out.flush();
return (buf.toString());