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

不打扰是莪最后的温柔 提交于 2020-01-22 18:58:08

问题


I have the below code with which i am able to print the fullclassname,classname,methodname, at which error occured.

Also, I am able to print Line-Number but the Line-Number printed is the line at which the variable "LineNumber" is initialized.

How can i print the exact LineNumber and ColumnNumber in try block at which error occured?

try
{
    SQL Query
}
catch(Exception e)
{
   String fullClassName = Thread.currentThread().getStackTrace()[1].getClassName();              
   String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);  
   String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();  
   int lineNumber = Thread.currentThread().getStackTrace()[1].getLineNumber();  

     JOptionPane.showMessageDialog(null,fullClassName+"--"+className+"--"+methodName+"--"+lineNumber,"Error In Moving data from table1 to table2",JOptionPane.ERROR_MESSAGE);                         

}

Ouput:

  IPM.Shifting--Shifting--ConfirmTransfer_BActionPerformed--1138

回答1:


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;
    }



}



回答2:


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



回答3:


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



回答4:


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)



回答5:


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


来源:https://stackoverflow.com/questions/14235687/can-we-get-linenumber-and-columnnumber-in-try-block-at-which-exception-occured

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!