내가 원하는 것은 오류가 발생한 클래스와 기능을 인쇄하는 사용자 정의 오류 메시지를 인쇄하는 것입니다. 클래스를 사용하려면 getClass()
을 사용하십시오. 그러나 나는 내 프로젝트를 실행하려고 할 때 다음과 같은 오류 메시지가 얻을 :형식의 메서드는 인수 (Class <Main>)에 적용 할 수 없습니까?
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.java
printException()
이처럼 보였다 여기서
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
@ 톰 그래, 그 이유는, 감사합니다 깨달았다! – 14wml