java - How to assure two vectors which will be passed in constructor size equals? -


i implemented class:

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     tableinfovector = new vector<tableinfo>();     for(int = 0; < tablenamevector.size(); i++)       tableinfovector.add(new tableinfo(tablenamevector.get(i), tabletagidvector.get(i)));   } } 

how elegant? throws exception? thanks.

personally, make method throw illegalargumentexception:

thrown indicate method has been passed illegal or inappropriate argument.

for example:

if (tablenamevector.size() != tabletagidvector.size())   throw new illegalargumentexception("tablenamevector , tabletagidvector " +                                      "must have same size"); 

even though illegalagumentexception unchecked exception, still add method's throws clause documentation.

making constructor throw exception prevent object being constructed, argue correct course of action in case.


Comments