throw
を使用して特定の例外をスローし、あとで処理してエラーメッセージをプレビューすることができます。例のため :
public void add(Appointment a){
if(a==null){
throw new IllegalArgumentException("appointement can't be null");
}
appointmentList.add(a);
}
あなたはまたRunTimeException
を拡張することにより、特定のAppointementAlreadyExistException
を作成し、それが必要とされるときにそれを投げることができます。
public class AppointementAlreadyExistException extends RuntimeException{
// constructors
}
と、次のようにそれを使用する:
public void add(Appointment a){
if(a==null){
throw new IllegalArgumentException("appointement can't be null");
}
//equals in appointement must be overrided to give correct behaviour
if(appointmentList!=null && appointmentList.contains(a)){
throw new AppointementAlreadyExistException();
}
appointmentList.add(a);
}
今この方法を呼び出すと、
// let's assume we have an Appointement a
try {
add(a)
}catch(AppointementAlreadyExistException ex){
//display the information to the user saying that appointment
}
私はこの記事やあなたが話しているコードでは本当に質問は表示されません。 – Stultuske
パブリッククラスAppointmentBook { プライベートstatic final int NOTFOUND = -1; プライベートArrayList予定リスト=新しいArrayList (); public void add(アポイントメントa){ \t appointmentList.add(a); \t \t } –
Pato
コードブロックを使用してコードを共有し、元のメッセージに入れてください。また、質問をしてください。 – Nico