繁体   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