Skip to Main Content
Java Enterprise Best Practices
book

Java Enterprise Best Practices

by O'Reilly Java Authors
December 2002
Intermediate to advanced content levelIntermediate to advanced
288 pages
9h 46m
English
O'Reilly Media, Inc.
Content preview from Java Enterprise Best Practices

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Moving to Java 9: Better Design and Simpler Code

Moving to Java 9: Better Design and Simpler Code

Trisha Gee
Java EE 8 High Performance

Java EE 8 High Performance

Romain Manni-Bucau

Publisher Resources

ISBN: 0596003846Supplemental ContentErrata Page