I have a static method in a utility class that assigns a value to a field.
Cutting version of my method:
public static void assignValue(String field, String valueToAssign){
...
}
If there is an error, I am currently throwing an exception, but I'm not sure if this is the right approach, because I am calling assignValuein a loop, and I want the loop to continue even if there is an exception. Therefore, you can potentially eliminate many exceptions.
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 be better if you assignValuereturned an error message instead of an exception? Then I can save the error and throw an exception when the list is complete.
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 to approach the error message, should I rename this method so that users know that the error message will be returned?
source