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

StringgetExceptionDescription(Throwable e)
Get the message + stack trace associated with this exception.
try {
    StringWriter stringWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(stringWriter);
    try {
        e.printStackTrace(printWriter);
        return stringWriter.getBuffer().toString();
    } finally {
        stringWriter.close();
...
StringgetExceptionDetails(final Throwable e)
get Exception Details
return getExceptionDetails(e, "");
StringgetExceptionDetails(Throwable throwable)
Get exception information as a string.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println("Exception: ");
throwable.printStackTrace(pw);
return sw.toString();
StringgetExceptionHeadline(Throwable t)
get Exception Headline
Pattern pattern = Pattern.compile("([\\w\\.]+)(:.*)?");
String stackTrace = getStackTrace(t);
Matcher matcher = pattern.matcher(stackTrace);
if (matcher.find())
    return matcher.group(1);
return null;
StringgetExceptionPrintout(Throwable aThrowable)
Defines a custom format for the stack trace as String.
final StringBuilder result = new StringBuilder();
result.append(aThrowable.toString());
final String NEW_LINE = System.getProperty("line.separator");
result.append(NEW_LINE);
for (StackTraceElement element : aThrowable.getStackTrace()) {
    result.append(element);
    result.append(NEW_LINE);
return result.toString();
StringgetExceptionStack(final Throwable e)
get Exception Stack
java.io.StringWriter wSW = new java.io.StringWriter();
e.printStackTrace(new java.io.PrintWriter(wSW));
return wSW.toString();
StringgetExceptionStack(Throwable e)
get Exception Stack
String msg = e.getMessage();
try {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    msg = sw.toString();
} catch (Throwable e2) {
    msg = "bad getErrorInfoFromException";
...
StringgetExceptionStackTrace(Throwable ex)
get Exception Stack Trace
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
pw.close();
return sw.toString();
StringgetExceptionStackTrace(Throwable exception)
get Exception Stack Trace
if (null == exception)
    throw new IllegalArgumentException("exception can't be null;");
String stack_trace;
StringWriter string_writer = new StringWriter();
try (PrintWriter print_writer = new PrintWriter(string_writer)) {
    exception.printStackTrace(print_writer);
    stack_trace = string_writer.getBuffer().toString();
return stack_trace;
StringgetExceptionStackTrace(Throwable exception)
Obtains the entire stracktrace of an exception and converts it into a string.
if (null == exception)
    throw new IllegalArgumentException("exception can't be null;");
String stack_trace = null;
StringWriter string_writer = new StringWriter();
PrintWriter print_writer = new PrintWriter(string_writer);
exception.printStackTrace(print_writer);
stack_trace = string_writer.getBuffer().toString();
print_writer.close();
...