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

voiddeeplyPrint(Throwable e, PrintStream strm, boolean stackTrace)
deeply Print
deeplyPrint(e, strm, stackTrace, 0);
voidstackTrace()
Create a fake stack trace
try {
    throw new Exception("STACK TRACE");
} catch (Exception e) {
    e.printStackTrace();
voidstackTrace()
Print the current stack trace.
StackTraceElement[] trace = Thread.currentThread().getStackTrace();
boolean b = false;
for (StackTraceElement e : trace) {
    System.out.println(e.getClassName() + "." + e.getMethodName() + "(" + e.getFileName() + ":"
            + e.getLineNumber() + ")");
    if (e.getMethodName().equals("stackTrace") && e.getClassName().endsWith("Utils")) {
        if (b) {
            return;
...
StringstackTrace()
stack Trace
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
StringBuilder buf = new StringBuilder();
for (StackTraceElement element : elements) {
    buf.append("\r\n   ").append(element.getClassName()).append(".").append(element.getMethodName())
            .append("(").append(element.getFileName()).append(":").append(element.getLineNumber())
            .append(")");
return buf.toString();
...
StringstackTrace()
stack Trace
StackTraceElement[] elements = Thread.currentThread().getStackTrace();
StringBuilder sb = new StringBuilder();
for (int i = 2; i < elements.length; i++) {
    StackTraceElement s = elements[i];
    sb.append("\tat " + s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":"
            + s.getLineNumber() + ")\n");
return sb.toString();
...
Stringstacktrace()
stacktrace
try {
    throw new Exception();
} catch (Exception e) {
    StackTraceElement[] trace = e.getStackTrace();
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i < trace.length; i++) {
        StackTraceElement stackTraceElement = trace[i];
        sb.append(stackTraceElement.toString());
...
voidstackTrace(final Exception t)
Since calling Exception#printStackTrace() is considered a bad practice, we wrap all these calls with this method to prevent static code analysis from complaning.
t.printStackTrace(System.err);
StringstackTrace(final Throwable caught)
stack Trace
StringBuffer sb = new StringBuffer(1024);
sb.append("<pre>");
stackTrace(caught, sb);
sb.append("</pre>");
return sb.toString();
voidstackTrace(int depth)
stack Trace
int offset = 3; 
if (depth == 0)
    depth = 4; 
StackTraceElement[] seArray = Thread.currentThread().getStackTrace();
if (seArray.length > offset) {
    System.out.println("Displaying " + depth + " of " + seArray.length + " stack trace elements"); 
    for (int index = offset; index < Math.min(depth + offset, seArray.length + offset); index++)
        System.out.println("   " + seArray[index].getClassName() + "." + seArray[index].getMethodName() 
...
StringstackTrace(StackTraceElement[] stackTrace)
stack Trace
StringBuilder str = new StringBuilder("");
for (StackTraceElement s : stackTrace) {
    str.append("\t").append(s.getClassName() + "." + s.getMethodName() + "(" + s.getFileName() + ":"
            + s.getLineNumber() + ")" + "\n");
return str.toString();