【问题标题】:Java custom exception handling in class类中的 Java 自定义异常处理
【发布时间】:2017-03-26 21:58:35
【问题描述】:

我正在使用 netbeans,并创建了这个异常类:

public class VehicleException extends Exception{
    private String matricula;
    private Calendar dataMatricula;
    private ModelVehicle model;

    private int causa;

        public VehicleException(int causa, Object valor) throws Exception {
        this.causa = causa;
        switch (causa) {
            case 1:
                dataMatricula = (Calendar) valor;
                break;
            case 2:
                matricula = (String) valor;
                break;
            case 3:
                model = (ModelVehicle) valor;
                break;

            default:
                throw new Exception("Utilització errònia en construir VehicleException. Causa: " + causa);
        }
    }

         @Override
    public String getMessage() {
        switch (causa) {
            case 1:
                return "dataMatricula erroni: " + dataMatricula + " Ha de contenir valor";
            case 2:
                return "matricula erroni: " + matricula + " Ha de contenir valor";
            case 3:
                return "Model erroni: " + model + " Ha de contenir valor";
            default:
                return "";
        }
    }

我正在我的“车辆”类中处理这种异常:

public class Vehicle implements Comparable<Vehicle> {

    private String matricula;
    private ModelVehicle model;
    private Calendar dataMatricula;


     public Vehicle(String matricula, ModelVehicle model, Calendar dataMatricula) throws VehicleException {
        setMatricula(matricula);
        setDataMatricula(dataMatricula);
        setModelVehicle(model);


    }


     public String getMatricula(){
         return matricula;
     }

     public ModelVehicle getModelVehicle(){
         return model;
     }
     public Calendar getDataMatricula(){
         return dataMatricula;
     }


      public final void setMatricula(String matricula) throws VehicleException {
        if (matricula == null || matricula.compareTo("") == 0) {
            throw new VehicleException(2,matricula);
        }
        this.matricula = matricula;
    }

      public void setModelVehicle(ModelVehicle model) throws VehicleException{
          if(model == null){
             throw new VehicleException(3,model);
          }
          else {
              this.model = model;
          }
      }

      public void setDataMatricula(Calendar c) throws VehicleException{
          if(c == null){
             throw new VehicleException(1,c);
          }
          this.dataMatricula = c;
      }

当我尝试编译时出现问题,在 setter 方法中我收到此消息:

错误:未报告的异常Exception;必须被抓住或宣布 被扔 抛出新的 VehicleException(2,matricula); C:\Users\Ivan\Desktop\Examen isidrer\M03-uf5\Exmaenm03uf5\src\info\infomila\Vehicle.java:55:错误: 未报告的异常异常;必须被抓获或被宣布为 抛出 抛出新的 VehicleException(3,model); C:\Users\Ivan\Desktop\Examen isidrer\M03-uf5\Exmaenm03uf5\src\info\infomila\Vehicle.java:64:错误: 未报告的异常异常;必须被抓获或被宣布为 抛出 抛出新的 VehicleException(1,c);

我不太明白为什么会这样,显然 setter 方法有一个“抛出 VehicleException”。

【问题讨论】:

    标签: java exception


    【解决方案1】:

    VehicleException 的构造函数本身可以抛出异常:

    public VehicleException(int causa, Object valor) throws Exception {
                                                     ^^^^^^^^^^^^^^^^
    

    这意味着您的调用范围需要从构造函数处理/重新抛出该异常,或者您可以选择 suppress the exception 而不是抛出它。

    default:
        super.addSuppressed(new Exception("Utilització errònia en construir VehicleException. Causa: " + causa));
    

    【讨论】:

    • 从传递给Exception构造函数的消息中,我通常倾向于在这里抛出RuntimeException
    • @Izruo 如果他们抛出RuntimeException,那么VehicleException 将永远不会被看到,因此这允许两个异常都出现在错误日志中。
    • 我的意思是Exception 通常是suppressed,当它是由于其他Exception 被抛出而引起的。当程序员在他的代码中使用错误的常量(在这种情况下是抛出Exception)时,这里会抛出这个。但是,当谈到日志记录时,你就明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多