Can we get LineNumber and ColumnNumber in try block at which exception occured

左心房为你撑大大i 提交于 2019-12-04 02:54:38
public class ExceptionHandler {

    /**
     * @param args
     */
    public static void main(String[] args) {

        try {
            String str = getString();
            if(str.isEmpty()){
                System.out.println("error");
            }
        } catch (Exception e) {
            StackTraceElement[] elements = e.getStackTrace();  
            for (int iterator=1; iterator<=elements.length; iterator++)  
                   System.out.println("Class Name:"+elements[iterator-1].getClassName()+" Method Name:"+elements[iterator-1].getMethodName()+" Line Number:"+elements[iterator-1].getLineNumber());
        }
    }

    private static String getString() {
        jhgfjkhgjh();
        return null;
    }

    private static void jhgfjkhgjh() {
        gfdhdfghdg();
    }

    private static void gfdhdfghdg() {
        sdfytusdgsfd();
    }

    private static void sdfytusdgsfd() {
        throw null;
    }



}
StackTraceElement[] stackTrace = Thread.currentThread()
                    .getStackTrace();
String fullClassName = stackTrace[stackTrace.length-1].getClassName();
String className = fullClassName.substring(fullClassName
        .lastIndexOf(".") + 1);
String methodName = stackTrace[stackTrace.length-1].getMethodName();
int lineNumber = stackTrace[stackTrace.length-1].getLineNumber();
} catch (Exception e) {            
        StackTraceElement[] stackTrace = e.getStackTrace();             
        String fullClassName = stackTrace[stackTrace.length - 1].getClassName();
        String className = fullClassName.substring(fullClassName
                .lastIndexOf(".") + 1);
        String methodName = stackTrace[stackTrace.length - 1].getMethodName();
        int lineNumber = stackTrace[stackTrace.length - 1].getLineNumber();
        JOptionPane.showMessageDialog(null, fullClassName + "--" + className + "--" + methodName + "--" + lineNumber, "Error In Moving data from table1 to table2", JOptionPane.ERROR_MESSAGE);
    }

You could also try

JOptionPane.showMessageDialog(null, ""+Thread.currentThread().getStackTrace()[1]);

e.g.

System.out.println(Thread.currentThread().getStackTrace()[1]);

prints

Main.main(Main.java:32)

Here , I made a solution hope It will help you .

public class TestApp{

    public static void main(String...strings){
        try{
             int num1=30, num2=0;
             int output=num1/num2;
             System.out.println ("Result: "+output);
          }
          catch(Exception e){

              ExceptionHandleCenter.throwException(e,
                      Thread.currentThread().getStackTrace()[1].getClassName(),
                      Thread.currentThread().getStackTrace()[1].getMethodName(),
                      e.getStackTrace()[0].getLineNumber());
          }
    }
}

class ExceptionHandleCenter {

    public static void throwException(Exception e, String fullClassName, String methodName,int lineNumber){

        String info = fullClassName+" method "+methodName+" at line "+lineNumber;

        if(e.getClass().equals(ArithmeticException.class)){
             System.err.println("Airthmetic exception. occurred In "+info);
         }
        if(e.getClass().equals(IllegalArgumentException.class)){
             System.err.println("Provided Argument is illegal.occurred In "+info);
         }
        if(e.getClass().equals(NumberFormatException.class)){
             System.err.println("Number Format Exception. occurred In "+info);
         }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!