0

サンプル用に、検証の種類ごとに特殊な例外を作成する必要があります。

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");
   ...
}
4

2 に答える 2

3

最初のアプローチを使用すると、呼び出し元は各例外を個別に処理できます。

try
{
    doSomething(new Text("blah blah"));
}
catch(NonAlphaNumericException e){/* do something */}
catch(BlankException e){/* do something else */}
于 2013-07-30T18:46:37.960 に答える