ビジネスロジックは例外の内側に行くが、非常に明示的な例外メッセージを持つことは、保守系を有するために重要であることはありません。ここで
public InvalidCommandException extends FormattedRuntimeException
{
public InvalidCommandException(@Nonnull final String command)
{
super("(%s) is not a valid command. Valid command are [print, sortfirst, sortlast, sortgrade]!", command);
}
}
はどのように入力を読んでいるFormattedRuntimeException
/**
* This is a convenience facade class to enable simple message creation as the String.format()
* facilities instead of manually building strings to pass in as a message.
* <p/>
*/
public class FormattedRuntimeException extends RuntimeException
{
protected FormattedRuntimeException(@Nonnull final String format, @Nonnull Object... args)
{
super(String.format(format, args));
}
protected FormattedRuntimeException(@Nonnull final Throwable cause, @Nonnull String format, @Nonnull Object... args)
{
super(String.format(format, args), cause);
}
/**
* This no-arg constructor is hidden specifically to keep people from using it
*/
private FormattedRuntimeException()
{ /* keep people from using "anonymous" instances */ }
/**
* This Exception only constructor is hidden specifically to keep people from using it
*/
private FormattedRuntimeException(final Exception e)
{ /* keep people from using "anonymous" instances */ }
}
のですか?コマンドライン引数? java.util.Scanner? –
ifを避けるために 'Arrays.asList(" print "、" sortfirst "etc).contains(message)'を使うことができます。しかし、とにかくやりたいことをするのは悪い考えです。 –
@JarrodRoberson私はスキャナーを使用していますが、これは既に提出して渡した古いプロジェクトです。異なる種類の例外処理を実行するだけです。 – GonePhisin