简体   繁体   中英

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() . 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 . B/c before I had:

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

where ExceptionPrinter.java printException() looked like this:

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!

Code

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

In java class is a keyword, So you can't declare like a variable, ie Change Class class to Class c or any

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. 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

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.
  • Added final because it's a good practice to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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