I have a series of exception messages:
enum myEnum {
BASIC(1, "An error occurred"),
WITH_ERRORID(2, "Error id: {0}"),
DETAILED(3, "Problem in record {0} with index {1}");
};
I also have a single, reusable authority for logging and throwing a custom exception:
public void throwException (myEnum message) {
log.addEntry(message);
throw new CustomException(message);
}
Calling the method is straight forward:
throwException (myEnum.DETAILED);
I am now wrestling with the most elegant way to format the strings. I could add a toString() method to the enum to format the string based on the number of inputs, and change throwException to accept a String instead:
String msg = message.toString(variable);
throwException (msg);
String msg2 = message.toString(variable, otherVariable);
throwException (msg2);
String msg3 = message.toString(variable, otherVariable, nextVariable);
throwException (msg3);
While this would work, I would like to move the the repeated toString calls to within throwException(). I was considering passing an ArrayList to throwException(). But I would then have to check the size of the list before I format the string:
if (list.size == 1) MessageFormat.format(message, list.get(0));
if (list.size == 2) MessageFormat.format(message, list.get(0), list.get(1));
Is a better technical or design approach for solving this problem?