FAQ

Java

JSP

Servlet


Advertisement



How to display stack trace information?

For example,

 

class ClassUtil {
   
    public String fullyQualifiedName;
    public ClassUtil(String fullyQualifiedName) {
        super();
        if (fullyQualifiedName == null)
            this.fullyQualifiedName = "";
        else {
            this.fullyQualifiedName = fullyQualifiedName.trim();
        }
    }
   
    public ClassUtil(Class c) {
       
        super();
        this.fullyQualifiedName = c.getName();
       
    }
    public String getFullClassName() {
        return this.fullyQualifiedName;
    }
   
    public String getPackageName() {
        int lastDot = fullyQualifiedName.lastIndexOf ('.');
        return (lastDot<=0)?"":fullyQualifiedName.substring (0, lastDot);
    }
        
    public String getClassName() {
        int lastDot = fullyQualifiedName.lastIndexOf ('.');
        return (lastDot<=0)?fullyQualifiedName:fullyQualifiedName.substring (++lastDot);
    }
   
}
public class StackTraceDisplay {
    public static void main(String[] args) {
        doFun1();
    }
    public static void doFun1(){
        doFun2();
    }
    public static void doFun2(){
        displayStackTrace(Thread.currentThread().getStackTrace());
    }
   
    public static void displayStackTrace(StackTraceElement e[]) {
       
        for (StackTraceElement s : e) {
            if (s.getMethodName().equals("getStackTrace"))
                continue;
            System.out.println ("Filename: " + s.getFileName());
            System.out.println ("Line number: " + s.getLineNumber());
            ClassUtil cu = new ClassUtil(s.getClassName());
            System.out.println ("Package name: " + cu.getPackageName());
            System.out.println ("Full Class name: " + cu.getFullClassName());
            System.out.println ("Classlass name: " + cu.getClassName());
            System.out.println ("Method name: " + s.getMethodName());
            System.out.println ("Native method?: " + s.isNativeMethod());
            System.out.println ("toString(): " + s.toString());
            System.out.println ("");
        }
    }
}


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |