Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top