append Throwable Cause - Java java.lang

Java examples for java.lang:Throwable

Description

append Throwable Cause

Demo Code


//package com.java2s;

public class Main {
    private final static String NL = "\n";

    private static void appendCause(final StringBuilder sb,
            Throwable cause, String prefix) {
        if (cause != null) {
            sb.append(prefix).append(": ").append(cause).append(NL);
            sb.append(prefix).append(".exception: ")
                    .append(cause.getClass()).append(NL);
            sb.append(prefix).append(".message: ")
                    .append(cause.getMessage()).append(NL);
            sb.append(prefix).append(".stacktrace: ").append(NL);
            appendStackTrace(sb, cause);
        }//from w  w  w . j a v a2  s  . c  o m
    }

    private static void appendStackTrace(final StringBuilder sb,
            final Throwable t) {

        StackTraceElement[] list = t.getStackTrace();
        if (list != null) {
            for (StackTraceElement ste : list) {
                sb.append("..").append(ste.toString()).append(NL);
            }
        } else {
            sb.append("..").append("<No stack Trace>").append(NL);
        }
    }
}

Related Tutorials