Java Utililty Methods Stacktrace

List of utility methods to do Stacktrace

Description

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

Method

StringstackTrace2String(Throwable e)
make exception stack trace become string
StringBuffer sb = new StringBuffer();
sb.append(e.toString()).append("\r\n");
StackTraceElement[] traces = e.getStackTrace();
for (StackTraceElement trace : traces) {
    sb.append(trace.toString()).append("\r\n");
return sb.toString();
booleanstackTraceApproved(StackTraceElement elt, String[] approved)
stack Trace Approved
String className = elt.getClassName();
for (int i = 0; i < approved.length; i++) {
    String s = approved[i];
    if (className.startsWith(s))
        return true;
return false;
StringstackTraceAsString(Throwable t)
stack Trace As String
StringBuilder sb = new StringBuilder();
sb.append(t.getClass().getName()).append(": ").append(t.getMessage());
for (StackTraceElement e : t.getStackTrace()) {
    sb.append("\n");
    sb.append(e.toString());
return sb.toString();
booleanstackTraceContainsCause(Throwable throwable, Class searchedCause)
Check if the stack trace of provided exception contains a given cause.
Throwable t = throwable;
while ((t = t.getCause()) != null) {
    if (searchedCause.isAssignableFrom(t.getClass())) {
        return true;
return false;
StringstackTraceElementToString(StackTraceElement element)
stack Trace Element To String
String methodName = element.getMethodName();
if (methodName == null || UNKNOWN.equals(methodName))
    return null;
String frame = "    at ";
String className = element.getClassName();
if (className != null && !UNKNOWN.equals(className))
    frame += className + ".";
frame += methodName + "(";
...
StringstackTraceElementWithLinenumber(StackTraceElement stackTraceElement)
Generates a string from trace for the current class with linenumber.
if (null != stackTraceElement) {
    return stackTraceElement.getClassName() + LINE_NUMBER_SEPARATOR + stackTraceElement.getLineNumber();
} else {
    return "Internal ERROR stackTraceElement should not be null";
StringstackTraceElementWithLinenumberAsString(StackTraceElement stackTraceElement)
stack Trace Element With Linenumber As String
if (null != stackTraceElement) {
    return stackTraceElement.getClassName() + LINE_NUMBER_SEPARATOR + stackTraceElement.getLineNumber();
} else {
    return "Internal ERROR stackTraceElement should not be null";
booleanstackTraceEquals(StackTraceElement[] ta1, StackTraceElement[] ta2)
Compares two stack traces and checks if they are equal
if (ta1 == ta2) {
    return true;
if (ta1.length == ta2.length) {
    for (int i = 0, lnLength = ta1.length; i < lnLength; i++) {
        if (!equals(ta1[i], ta2[i])) {
            return false;
    return true;
return false;
StringstackTraceGenerate()
stack Trace Generate
StackTraceElement[] steArr = Thread.currentThread().getStackTrace();
String ret = "";
for (StackTraceElement ste : steArr) {
    ret += ste.toString() + "\n";
return ret;