簡體   English   中英

類型中的方法不適用於參數(類 <Main> )?

[英]The method in the type is not applicable for the arguments (Class<Main>)?

我要執行的操作是打印一條自定義錯誤消息,該消息將打印導致錯誤的類和函數。要獲取該類,請使用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)

而且我不知道為什么不能使用.classClass傳遞給函數。 我之前的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);
    }

}

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

在java中class是一個關鍵字,因此您不能像變量一樣聲明,即將Class class更改為Class 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);
    }
}

似乎您的ExceptionPrinter不是您使用的最新編譯版本,並且它的類可能只有public static void printException(Exception e)方法,而沒有其他方法。 首先編譯。 如果您將使用編譯所有依賴代碼的構建工具,則默認情況下,您將不會看到這一點。

異常ExceptionPrinter類型The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class<Main>, String, String)建議未找到其他重載方法

將重載的方法簽名更改為:

public static void printException(
    final Exception execption, 
    final Class clazz, 
    final String function, 
    final String message)
  • class是Java中的保留字,不能用作參數名稱。
  • 添加了final因為這是一種很好的做法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM