Don’t Throw RemoteException in Your Server Code
The
javadocs for RemoteException say the following:
A RemoteException is the common superclass for a number of communication-related exceptions that may occur during the execution of a remote method call. Each method of a remote interface, an interface that extends java.rmi.Remote, must list RemoteException in its throws clause.
This might make it seem like it’s OK, and maybe even
a good thing, for your server-side code to throw instances of
RemoteException. It’s certainly
easy, if you’re working on a server and discover a
new exceptional condition, to add a line of code such as the
following:
throw new RemoteException("You can't deposit a negative amount of money");It might even seem like good programming practice—after all,
the client code already catches RemoteException.
But it’s a very bad idea to use
RemoteException in this way.
To understand why, you need to understand what
RemoteException really means. The real meaning of
RemoteException is that something has gone wrong
between your client code and server code. That
is, your client made a method call on a stub. Your server code is
expecting to receive a method invocation via its skeleton. If
something goes wrong between that call to the stub and the resulting
invocation made by the skeleton, it will be signalled by an instance
of RemoteException. Exceptions that happen within
the server should be signalled by instances of some other exception
class that doesn’t extend
RemoteException ...