I have a static method in a utility class which assigns a value to a field.
A cutdown version of my method is:
public static void assignValue(String field, String valueToAssign){
...
//code to do assign
}
If there is an error I am currently throwing an exception however I am unsure if that is the right approach because I call assignValue
in a loop and I want the loop to continue even if there is an exception. So potentially many exceptions can be thrown
for(int i = 0; i < assignmentList.size(); i++){
try{
assignValue(assignmentList.get(i).fieldName, assignmentList.get(i).fieldValue);
}catch(AssignmentFailedException e){
errorlist.add(e.getMessage());
}
}
}
Would it better better if assignValue
returned an error message instead of an exception? I can then store the error and throw an exception when the list is finished
for(int i = 0; i < assignmentList.size(); i++){
String errorMessage = assignValue(assignmentList.get(i).fieldName, assignmentList.get(i).fieldValue);
if(errorMessage != ""){
errorlist.add(errorMessage);
}
}
If I go for the error message approach should I rename the method to let users know an error message will be returned?