Java Utililty Methods Exception to String

List of utility methods to do Exception to String

Description

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

Method

Stringexception2string(Throwable exception)
exceptionstring
StackTraceElement[] es = exception.getStackTrace();
StringBuilder builder = new StringBuilder();
builder.append(exception.toString());
for (StackTraceElement e : es) {
    builder.append("\n\tat ");
    builder.append(e.toString());
return builder.toString();
...
Stringexception2StringShort(Exception e)
Extrahieren der root-Exception aus einer Exception-Chain.
StringBuffer st = new StringBuffer();
Throwable e2 = e;
while (e2 != null) {
    String exClass = e2.getClass().getName();
    String msg = e2.getMessage();
    if (msg != null) {
        st.setLength(0);
        st.append(exClass);
...
Stringexception_details(Exception e)
exceptiodetails
e.printStackTrace();
return "Exception thrown: " + e.getClass().getName() + " : " + e.getMessage();
Stringexception_details(Exception e, String message)
Prepare an exception string suitable for presentation (no stack trace).
if (e == null) {
    return "";
e.printStackTrace(); 
String emessage = e.getMessage();
if (emessage == null) {
    emessage = "No Message";
return "Exception: " + e.getClass().getName() + "\n" + ((message != null) ? message + "\n" : "")
        + emessage.replaceAll("<", "&lt;");
StringexceptionFormat(E exception)
exception Format
Exception e = (Exception) exception;
Integer line = null;
String className = null;
String methodName = null;
if (e.getStackTrace() != null && e.getStackTrace().length > 2) {
    line = e.getStackTrace()[2].getLineNumber();
    className = e.getStackTrace()[2].getClassName();
    methodName = e.getStackTrace()[2].getMethodName();
...
StringexceptionStackToString(Throwable x)
exception Stack To String
if (x == null)
    return ("NULL exception!");
return (throwableToString(x, ""));
StringexceptionToString(final Exception e)
exception To String
return "<span style='color:red; font-size:10px;'>" + e.toString() + "</span>";
StringexceptionToString(final Throwable ex)
exception To String
return exceptionToString(ex, "; ");
StringexceptionToString(final Throwable throwable)
exception To String
StringBuilder result = new StringBuilder();
Throwable cause = throwable;
while (cause != null) {
    if (result.length() == 0) {
        result.append("\nException in thread.");
    } else {
        result.append("\nCaused by: ");
    result.append(cause.getClass().getCanonicalName()).append(": ").append(cause.getMessage());
    for (final StackTraceElement traceElement : cause.getStackTrace()) {
        result.append("\n\tat ").append(traceElement.toString());
    cause = cause.getCause();
return result.toString();
StringexceptionToString(Throwable e)
Returns a String representation of Exception + stacktrace
return exceptionToString(e, false);