I am new to using RMI and I am relatively new to using exceptions.
I want to be able to throw an exception over RMI (is this possible?)
I have a simple server which serves up students and I have delete method which if the student doesn't exist I want to throw a custom exception of StudentNotFoundException which extends RemoteException (is this a good thing to do?)
Any advice or guidance would be greatly appreciated.
Server Interface method
/**
* 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;
Server method implementation
@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");
}
}
Client method
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);
}
}
Thank you for your reply I have now managed to fix my problem and realised that extending RemoteException was bad idea.
ServerException. This exception is thrown as a result of a remote method invocation when a RemoteException is thrown while processing the invocation on the server, either while unmarshalling the arguments or executing the remote method itself.
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM. RMI is used to build distributed applications; it provides remote communication between Java programs. It is provided in the package java. rmi.
The RMI architecture is based on a very important principle which states that the definition of the behavior and the implementation of that behavior, are separate concepts. RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.
It's OK to throw any kind of exception (even custom ones), just make sure to package them up in your export .jar file (if you're using a version of Java where you need to do this manually).
I wouldn't subclass RemoteException, though. Those are typically thrown if there is some kind of connection problem. Presumably, your client will handle connection problems differently from other types of problems. You might tell the user the server is down when you catch a RemoteException, or connect to a different server. For StudentNotFoundException, you probably want to give the user another chance at entering the student info.
Yes, it's possible to throw exceptions via RMI.
No, it's not a good idea to extend RemoteException
to report application failures. RemoteException
signals a failure in the remoting mechanism, like a network failure. Use an appropriate exception, extending java.lang.Exception
yourself if necessary.
For a more detailed explanation, look at another of my answers. In a nutshell, be careful about chaining exceptions when using RMI.
I want to be able to throw an exception over RMI (is this possible?)
Yes. Anything can be serialized, even exceptions. I think Exception itself implements Serializable.
I have a simple server which serves up students and I have delete method which if the student doesn't exist I want to throw a custom exception of StudentNotFoundException which extends RemoteException (is this a good thing to do?)
I would have it extend Exception personally. Your exceptions are your exceptions, and RemoteExceptions are for things that go wrong with RMI for connectivity reasons.
There is no need for your exceptions to extend RemoteException
.
(It's worth noting that concrete exception types thrown need to be in codebases used by both the server and the client.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With