私は RMI を初めて使用し、例外を使用するのは比較的初めてです。
RMI 経由で例外をスローできるようにしたい (これは可能ですか?)
学生にサービスを提供する単純なサーバーがあり、学生が存在しない場合は、RemoteException を拡張する StudentNotFoundException のカスタム例外をスローする delete メソッドがあります (これは良いことですか?)
アドバイスやガイダンスをいただければ幸いです。
サーバーインターフェース方式
/**
* Delete a student on the server
*
* @param id of the student
* @throws RemoteException
* @throws StudentNotFoundException when a student is not found in the system
*/
void removeStudent(int id) throws RemoteException, StudentNotFoundException;
サーバーメソッドの実装
@Override
public void removeStudent(int id) throws RemoteException, StudentNotFoundException
{
Student student = studentList.remove(id);
if (student == null)
{
throw new StudentNotFoundException("Student with id:" + id + " not found in the system");
}
}
クライアント方式
private void removeStudent(int id) throws RemoteException
{
try
{
server.removeStudent(id);
System.out.println("Removed student with id: " + id);
}
catch (StudentNotFoundException e)
{
System.out.println(e.getMessage());
}
}
StudentNotFoundException
package studentserver.common;
import java.rmi.RemoteException;
public class StudentNotFoundException extends RemoteException
{
private static final long serialVersionUID = 1L;
public StudentNotFoundException(String message)
{
super(message);
}
}
ご返信いただきありがとうございます。問題を解決することができ、RemoteException を拡張することは悪い考えであることに気付きました。