2017-10-01 2 views
0

내가 원하는 것은 오류가 발생한 클래스와 기능을 인쇄하는 사용자 정의 오류 메시지를 인쇄하는 것입니다. 클래스를 사용하려면 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); 
    } 

} 
+2

'ExceptionPrinter' 클래스에서 컴파일러 문제를 무시할 수 있습니다.'class'는 유효한 변수 이름이 아닙니다. – Tom

+1

@ 톰 그래, 그 이유는, 감사합니다 깨달았다! – 14wml

답변

1

는 Main.java

public class Main { 
    public static void main(String[] args) { 
     try { 
      int a=2/0; 
     } catch (ArithmeticException e) { 
      ExceptionPrinter.printException(e, Main.class, "main", "Input file not found"); 
     } catch (NullPointerException e) { 
      ExceptionPrinter.printException(e, Main.class, "main", "Exception occurred during parsing"); 
     } 
    } 

} 

자바 클래스에서 ExceptionPrinter.java

는 키워드입니다, 그래서 당신은 변수, 즉, 변경 같이 선언 할 수 없습니다 종류 종류 to 종류 c 또는 어떤

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 c, String function, String message){ 
     //System.err.println(class + " " + function + "(): " + message); 
     printException(e); 
    } 
} 
0

은 ExceptionPrinter 사용중인 최신 컴파일 된 버전이 아닌 것 같은데 아마도 그것은 클래스는이다 public static void printException(Exception e) 방법이 있고 다른 것은 아닙니다. 네. 이것을 먼저 컴파일하십시오. 모든 종속 코드를 컴파일하는 빌드 도구를 사용했다면 기본적으로이 코드를 보지 못했을 것입니다.

예외 The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class<Main>, String, String) 제안 다른 오버로드 된 메서드는

0
가 당신의 오버로드 된 메서드 서명을 변경

발견되지 않은 :

public static void printException(
    final Exception execption, 
    final Class clazz, 
    final String function, 
    final String message) 
  • class 자바의 예약어이며, 매개 변수 이름으로 사용할 수 없습니다 .
  • final을 추가 했으므로 사용하는 것이 좋습니다.