简体   繁体   English

类型中的方法不适用于参数(类 <Main> )?

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

What I want to do is print a custom error message that prints the class and function that the error resulted in. To get the class I use getClass() . 我要执行的操作是打印一条自定义错误消息,该消息将打印导致错误的类和函数。要获取该类,请使用getClass() However when I try to run my project I get the following error message: 但是,当我尝试运行我的项目时,出现以下错误消息:

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)

And I don't know why I can't pass in Class into the function using .class . 而且我不知道为什么不能使用.classClass传递给函数。 B/c before I had: 我之前的B / c:

ExceptionHandler.printException(e, getClass() + " main() could not find input file");

where ExceptionPrinter.java printException() looked like this: ExceptionPrinter.java printException()如下所示:

public static void printException(Exception e, String message){
    System.err.println(message);
    printException(e);
}

And that worked fine. 而且效果很好。

If anyone could help me so that I can pass in the class name to my ExceptionPrinter.java that would be great! 如果有人可以帮助我,以便我可以将类名称传递给我的ExceptionPrinter.java ,那就太好了!

Code

Main.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 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 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 ExceptionPrinter.java

In java class is a keyword, So you can't declare like a variable, ie Change Class class to Class c or any 在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);
    }
}

Seems your ExceptionPrinter is not the latest compiled version you are using and probably it's class only has public static void printException(Exception e) method and not the other one. 似乎您的ExceptionPrinter不是您使用的最新编译版本,并且它的类可能只有public static void printException(Exception e)方法,而没有其他方法。 Compile this first. 首先编译。 If you would have used build tools that compiles all dependent code, by default then you would not have seen this. 如果您将使用编译所有依赖代码的构建工具,则默认情况下,您将不会看到这一点。

Exception The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class<Main>, String, String) suggest other overloaded method is not found 异常ExceptionPrinter类型The method printException(Exception) in the type ExceptionPrinter is not applicable for the arguments (FileNotFoundException, Class<Main>, String, String)建议未找到其他重载方法

Change your overloaded method signature to: 将重载的方法签名更改为:

public static void printException(
    final Exception execption, 
    final Class clazz, 
    final String function, 
    final String message)
  • class is a reserved word in Java and can't be used as a parameter name. class是Java中的保留字,不能用作参数名称。
  • Added final because it's a good practice to use. 添加了final因为这是一种很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM