我是使用RMI的新手,我使用異常比較新。RMI和例外
我希望能夠通過RMI拋出一個異常(這可能嗎?)
我有擔任了學生一個簡單的服務器,我有刪除的,如果學生不存在,我想方法扔StudentNotFoundException的自定義異常延伸的RemoteException(這是一個很好的事情?)
任何建議或指導,將不勝感激。
服務器接口方法
/**
* 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是個壞主意。
嘿,這個問題看起來很熟悉! – 2009-03-04 21:43:21
我在發帖前實際上看過這個。歡呼的建議 - 我認爲我現在已經解決了這個問題 – Malachi 2009-03-04 21:44:12