【发布时间】:2017-10-01 02:18:14
【问题描述】:
我想要做的是打印一个自定义错误消息,该消息打印错误导致的类和函数。为了获得类,我使用getClass()。但是,当我尝试运行我的项目时,我收到以下错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class<Main>, String, String)
The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (ParseException, Class<Main>, String, String)
at Main.main(Main.java)
而且我不知道为什么我不能使用.class 将Class 传递到函数中。我之前的 B/c:
ExceptionHandler.printException(e, getClass() + " main() could not find input file");
ExceptionPrinter.javaprintException() 看起来像这样:
public static void printException(Exception e, String message){
System.err.println(message);
printException(e);
}
而且效果很好。
如果有人可以帮助我,以便我可以将类名传递给我的 ExceptionPrinter.java,那就太好了!
代码
Main.java
public class Main {
public static void main(String[] args) {
try {
...
} catch (FileNotFoundException e) {
ExceptionPrinter.printException(e, Main.class, "main", "Input file not found");
} catch (ParseException e) {
ExceptionPrinter.printException(e, Main.class, "main", "Exception occurred during parsing");
}
}
}
ExceptionPrinter.java
public class ExceptionPrinter {
public static void printException(Exception e){
System.err.println("Error message: " + e.getMessage());
e.printStackTrace();
}
public static void printException(Exception e, Class class, String function, String message){
System.err.println(class + " " + function + "(): " + message);
printException(e);
}
}
【问题讨论】:
-
您可以停止忽略
ExceptionPrinter类中的编译器问题。class不是有效的变量名。 -
@Tom 是的,我意识到这就是原因,谢谢!
标签: java class exception printing error-handling