我是使用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