我是否应该为每种验证创建一个专门的异常,例如:
public void doSomething(Text text) {
if (!text.isAlphaNumeric()) throw new NonAlphaNumericException("Text should be alphanumeric");
if (text.isBlank()) throw new BlankException("Text should not be empty or null");
...
}
或者,我应该做一个通用的例外,比如:
public void doSomething(Text text) {
if (!text.isAlphaNumeric()) throw new TextValidationException("Text should be alphanumeric");
if (text.isBlank()) throw new TextValidationException("Text should not be empty or null");
...
}