9

我有下面的代码,我可以使用它打印发生错误的完整类名、类名、方法名。

另外,我可以打印行号,但打印的行号是初始化变量“行号”的行。

如何在发生错误的 try 块中打印确切的 LineNumber 和 ColumnNumber?

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

}

输出:

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

5 回答 5

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



}
于 2013-12-13T10:21:33.517 回答
2
} 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);
    }
于 2014-07-29T18:15:15.193 回答
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();
于 2013-01-09T12:59:39.003 回答
1

你也可以试试

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

例如

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

印刷

Main.main(Main.java:32)
于 2013-01-09T13:08:03.363 回答
0

在这里,我提出了一个解决方案,希望对你有所帮助。

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);
         }
    }
}
于 2018-03-22T11:59:09.297 回答