I would like to have error codes in my Java project, but they should have variable String descriptions. For example, I might have two BadCharacterCount errors, but I’d like to represent them as two different strings, e.g. “Bad character count: expected 50, got 60” and “Bad character count: expected 40, got 70.” I’d like to be able to recognize both of these cases as BadCharacterCount errors, and ideally have these in an enum of some sort.
There’s a nice discussion of a similar problem here:
Best way to define error codes/strings in Java?
However, they don’t deal with the case of variable error Strings… Any ideas?
I think your two error descriptions:
And:
Are already the same. The string is:
Then when you need to construct the error message, just make sure to pass it through
String.format:And