Saturday, 4 July 2020

How to display stack trace information in java programming language?

How to display stack trace information in java programming language?

here given a good example of source code ,by using source code you can
handle data of display of stack trace information.

 

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 ("");
        }
    }
}

0 comments:

Post a Comment