throw expression;という具合になる. 例外をスローするメソッドには,throws節を宣言する必要がある. メソッドは,メソッド名と両カッコの間の引数のパラメタをシグニチャ (signature)というが,例外をスローする場合には,シグニチャの後に throws節を付ける.複数の例外をスローする可能性がある場合には, それらを列挙する.
public void replaceValue(String anme, Object newValue) throws NoSuchAttributeException { Attr attr = find(name); // attrを探す. if (attr == null) // 見つからない場合 throw new NoSuchAttributeException(name); attr.setValue(newValue); }