如何确保两个将在构造函数大小中传递的向量等于?

时间:2015-05-08 09:30:15

标签: java exception constructor

我实现了一个类:

public class TableInfoGroup {
  Vector<TableInfo> tableInfoVector;
  public TableInfoGroup(Vector<String> tableNameVector, Vector<String> tableTagIdVector)
  {
    if (tableNameVector.size() != tableTagIdVector.size())
      return;//I think it's not proper to do this
    tableInfoVector = new Vector<TableInfo>();
    for(int i = 0; i < tableNameVector.size(); i++)
      tableInfoVector.add(new TableInfo(tableNameVector.get(i), tableTagIdVector.get(i)));
  }
}

如何做到优雅?抛出异常?感谢。

1 个答案:

答案 0 :(得分:7)

就个人而言,我会让方法抛出IllegalArgumentException

  

抛出以表明方法已被传递为非法或不恰当的参数。

例如:

IllegalAgumentException

即使ClassAunchecked exception,我仍然会将其作为文档添加到方法throws clause中。

使构造函数抛出异常会阻止构造对象,在这种情况下,我认为这是正确的操作过程。