Java中简单提示异常代码的行号,类名等

public class Test1 {

    public static void main(String args[]) {
        System.out.println(getLineInfo());
        getExp();
    }

    public static String getLineInfo() {
        StackTraceElement ste = new Throwable().getStackTrace()[1];
        return ste.getFileName() + ": Line " + ste.getLineNumber();
    }

    public static String getExp(){
        try{
            throw new Exception("Exception Test");
        }catch(Exception e){
            StackTraceElement ste =e.getStackTrace()[0];
            System.out.println(ste.getClassName());
            System.out.println(ste.getFileName());
            System.out.println(ste.getLineNumber());
            System.out.println(ste.getMethodName());
        }
        return null;
    }
}

 

posted @ 2017-03-27 09:27  太潮了就要晒  阅读(3459)  评论(0编辑  收藏  举报