【问题标题】:The method in the type is not applicable for the arguments (Class<Main>)?类型中的方法不适用于参数(Class<Main>)?
【发布时间】: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)

而且我不知道为什么我不能使用.classClass 传递到函数中。我之前的 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


【解决方案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

在java中类是关键字,所以不能像变量一样声明, 即将 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);
    }
}

【讨论】:

    【解决方案2】:

    似乎您的 ExceptionPrinter 不是您正在使用的最新编译版本,并且可能它的类只有 public static void printException(Exception e) 方法而不是另一个。先编译这个。如果你会使用编译所有依赖代码的构建工具,那么默认情况下你不会看到这个。

    Exception The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class&lt;Main&gt;, String, String) 提示未找到其他重载方法

    【讨论】:

      【解决方案3】:

      将您的重载方法签名更改为:

      public static void printException(
          final Exception execption, 
          final Class clazz, 
          final String function, 
          final String message)
      
      • class是Java中的保留字,不能作为参数名。
      • 添加了final,因为这是一种很好的使用习惯。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 1970-01-01
        • 1970-01-01
        • 2021-08-31
        • 2014-06-26
        相关资源
        最近更新 更多