Warning! RemoteException is checked, but not expected!

Normally when we think of expected vs. unexpected exceptions, we map it to checked vs. unchecked exceptions. A FileIOException is expected. A NullPointerException is not. Any direct subclass of Exception is expected. A subclass of RuntimeException is not. A checked exception must be handled. An unchecked exception usually won’t be.

But there’s one big exception to the whole exceptions and expectations thing—java.rmi.RemoteException.

RemoteException is a checked exception, of course, so the client is forced to acknowledge a RemoteException by handling it with a try/catch. But unlike the other checked exceptions a client sees in a bean’s interface, RemoteException is still considered unexpected. OK, not exactly unexpected... but unexpected.

Application exceptions are always compiler-checked exceptions, except for RemoteException.

Think of RemoteException as “a runtime exception on the server”, even though RemoteException is a checked exception to the client.

In other words, a remote client must EXPECT that the server can throw something UNEXPECTED.

From the client’s point of view, think of RemoteException a kind of runtime exception on the remote part of the application. Because that’s often what it means. A NullPointerException on the server means a RemoteException to the client. A DivideByZeroException on the server means a RemoteException to the client. A ClassCastException on the server means a RemoteException to the client. Those ...

Get Head First EJB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.