get Stack Trace - Java java.lang

Java examples for java.lang:Throwable

Description

get Stack Trace

Demo Code


//package com.java2s;
import java.util.regex.Pattern;

public class Main {
    static public String getStackTrace(Throwable t, String filterExp) {
        Pattern filter = null;/*w  ww . ja  va 2  s.c  o  m*/
        if (filterExp != null)
            Pattern.compile(filterExp, Pattern.CASE_INSENSITIVE);
        StringBuilder b = new StringBuilder();
        b.append(t.getMessage()).append("\n");
        StackTraceElement[] elements = t.getStackTrace();
        for (StackTraceElement element : elements) {
            String eleString = element.toString();
            if (filter != null && !filter.matcher(eleString).matches())
                continue;
            b.append(element.toString()).append("\n");
        }
        return b.toString();
    }

    static public String getStackTrace(StackTraceElement[] elements,
            String filterExp) {
        Pattern filter = null;
        if (filterExp != null)
            filter = Pattern.compile(filterExp, Pattern.CASE_INSENSITIVE);
        StringBuilder b = new StringBuilder();
        for (StackTraceElement element : elements) {
            String eleString = element.toString();
            if (filter != null && !filter.matcher(eleString).matches())
                continue;
            b.append(element.toString()).append("\n");
        }
        return b.toString();
    }
}

Related Tutorials