我有以下代码,我可以打印出发生错误的fullclassname,classname,methodname。
此外,我可以打印行号,但打印的行号是变量“LineNumber”初始化的行。
如何在发生错误的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
答案 0 :(得分: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;
}
}
答案 1 :(得分: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();
答案 2 :(得分: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);
}
答案 3 :(得分:1)
您也可以尝试
JOptionPane.showMessageDialog(null, ""+Thread.currentThread().getStackTrace()[1]);
e.g。
System.out.println(Thread.currentThread().getStackTrace()[1]);
打印
Main.main(Main.java:32)
答案 4 :(得分: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);
}
}
}