私は前提条件とそれらをいつ使用するかについて学んでいます。前提条件は
@pre fileName must be the name of a valid file
次のコードには適合しません。
/**
Creates a new FileReader, given the name of file to read from.
@param fileName- the name of file to read from
@throw FileNotFoundException - if the named file does not exist,
is a directory rather than a regular file, or for some other reason cannot
be opened for reading.
*/
public FileReader readFile(String fileName) throws FileNotFoundException {
. . .
}//readFile
どうしてこれなの?
編集:別の例
例として、次のことが「正しい」方法で行われると想定しています。IllegalArgumentExceptionと前提条件に注意してください。動作がどのように明確に定義されているか、および前提条件が設定されている場合でもthrows宣言がどのように行われるかに注意してください。最も重要なことは、NullPointerExceptionの前提条件が含まれていないことに注意してください。もう一度、なぜそうではないのですか?
/**
* @param start the beginning of the period
* @param end the end of the period; must not precede start
* @pre start <= end
* @post The time span of the returned period is positive.
* @throws IllegalArgumentException if start is after end
* @throws NullPointerException if start or end is null
*/
public Period(Date start, Date end) f
これらの例は、余分な前提条件の使用を回避していますか?前提条件を回避しているのなら、なぜそれらを持っているのでしょうか。つまり、すべての前提条件を@throws宣言に置き換えてみませんか(それらを回避することがここで行われている場合)?