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;
    }
}
相關文章
相關標籤/搜索