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

Java 类型中的方法不适用于参数(Class<;Main>;)?,java,class,exception,printing,error-handling,Java,Class,Exception,Printing,Error Handling,我要做的是打印一条自定义错误消息,该消息打印错误导致的类和函数。要获取类,我使用getClass()。但是,当我尝试运行我的项目时,会收到以下错误消息: Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method printException(Exception) in the type ExceptionPrinter is not applicab

我要做的是打印一条自定义错误消息,该消息打印错误导致的类和函数。要获取类,我使用
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)
其中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");
        }
    }

}
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);
    }
    
}
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");
        }
    }

}
例外打印机.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");
        }
    }

}
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);
    }
    
}
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不是您正在使用的最新编译版本,并且它的类可能只有
公共静态void printException(Exception e)
方法,而没有其他方法。首先编译这个。如果您使用编译所有依赖代码的构建工具,那么默认情况下您不会看到这一点


Exception
类型ExceptionPrinter中的方法printException(Exception)不适用于参数(FileNotFoundException,Class,String,String)
建议未找到其他重载方法

将重载方法签名更改为:

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

    • 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");
              }
          }
      
      }
      
      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);
          }
          
      }
      
      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");
              }
          }
      
      }
      
      例外打印机.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");
              }
          }
      
      }
      
      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);
          }
          
      }
      
      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");
              }
          }
      
      }
      
      在java中,类是一个关键字,所以不能像变量一样声明, i、 e将等级更改为等级c或任何


      您可以停止忽略
      ExceptionPrinter
      类中的编译器问题
      class
      不是有效的变量名。@Tom是的,我意识到这就是原因,谢谢!