【问题标题】:How to validate input through constructor in java如何通过java中的构造函数验证输入
【发布时间】:2018-05-23 17:11:36
【问题描述】:

我已经读到可以创建一个构造函数来检查输入是否正确,我尝试使用 IllegalArgumentException 来执行此操作。只要我不创建对象,我的代码就可以正常工作,无论输入是否正确,编译器都会出错。这是我的代码:

public class Knoten{
private String typ;

public Knoten(String typ) throws Exception{
  try{
    if (typ.equals ("BASE")||typ.equals ("WORD")||typ.equals ("ROOT")||typ.equals ("AFFIX")){
      this.typ=typ;
    }}catch(IllegalArgumentException ex){
       System.out.println ("invalid typ");
    }}
  public String getTyp(){
        return typ;
    }

  public String toString(){
        return "typ: "+getTyp();
    }

public static void main (String args[]){
Knoten Knot1= new Knoten("haha");//throws error
Knoten Knot2= new Knoten("BASE");//throws error
}}

我也尝试过这样做,得到了相同的结果:

public class Knoten{
  private String typ;

  public Knoten(String typ) throws Exception{
    if (typ.equals ("BASE")||typ.equals ("WORD")||typ.equals ("ROOT")||typ.equals ("AFFIX")){
        this.type=type;
    }else{
         throw new IllegalArgumentException("invalid type");
    }

    }
  public String getTyp(){
    return typ;
}

  public String toString(){
    return "typ: "+getTyp();
}

public static void main (String args[]){
Knoten Knot1= new Knoten("haha");//throws error
Knoten Knot2= new Knoten("BASE");//throws error

}

}

所以我的问题是我是否必须在每次创建该类的对象时处理异常,或者是否有更好的方法。

【问题讨论】:

  • type 是什么?你的变量名是typ。试试typ.equals("BASE")
  • 是的,对不起,我编辑了它,这不是问题。
  • 第一种方法行不通。你没有抛出异常。您是否尝试过调试第二个场景?

标签: java validation exception exception-handling


【解决方案1】:

我相信您会遇到编译错误,例如:Unhandled exception type Exception,而您在从constructor 抛出Checked Exception (java.lang.Exception) 时会遇到此错误。不要将任何异常放入 throws 或尝试抛出未经检查的异常,例如:

public Knoten(String type) throws IllegalArgumentException { // throws unchecked exception is optional
    if (type.equals("BASE") || type.equals("WORD") || type.equals("ROOT") || type.equals("AFFIX")) {
        this.type = type;
    } else {
        throw new IllegalArgumentException("invalid type");
    }

}

或者包围它构造函数try catch/finally

try {
    Knoten Knot1 = new Knoten("haha");
} catch (Exception e) {
    e.printStackTrace();
}

【讨论】:

    【解决方案2】:

    您需要在实例化新对象时捕获异常,在这种情况下,您的第二个解决方案很好,除了在 main 上您会这样做:

    public static void main (String args[]){
        try {
            Knoten Knot1= new Knoten("haha"); // This will throw error
            Knoten Knot2= new Knoten("BASE");
        }
        catch (IllegalArgumentException e) {
            // deal with exception here
        }
    }
    

    另外,我真的会考虑使用enums 作为您的解决方案,这样您在创建新对象时就不需要验证您的类型。例如,您可以有这样的enum

    public enum KnotenType {
         BASE,
         WORD,
         ROOT,
         AFFIX,
    }
    

    那么你的构造函数应该是这样的:

    public Knoten(KnotenType type) {
        ...
    }
    

    更多关于枚举的信息:https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

    【讨论】:

      【解决方案3】:

      您的代码中有拼写错误。使用

      this.typ = typ; // instead of this.type = type; 
      

      还要记住捕获构造函数抛出的异常

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-26
        • 1970-01-01
        • 2022-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多