Freitag, 4. Juni 2010

RemoteException blues

How java.rmi.RemoteException cripples the whole Jave RMI system. ...


As said before I am working on the
Sun Certified Java Developer (SCJD). Part of the
assignment needs to be implemented either with socket communication
or with RMI (remote method
invocation)
. I begin to think they added that option so
that all future “developers” find out just how badly Sun screwed
up in the RMI design.


So who did Sun screw up? Well all
methods using RMI might raise an java.rmi.RemoteException.
This in itself is a sensible requirement.


Now as any “Sun Certified Java
Programmer” knows Java distinguishes between three different
exception types: “checked”, “runtime”
and “error”. The first must be specified in the a
throws clause and must be handled by the client code. For the other
two specification and handling is optional.


For example
java.lang.NullPointerException and
java.lang.IllegalArgumentException are runtime
exceptions. Which is sensible as they can happen anywhere and having
to specify then everywhere makes no sense.


The official
guideline for a runtime exception
is:


Runtime exceptions represent
problems that are the result of a programming problem, and as such,
the API client code cannot reasonably be expected to recover from
them or to handle them in any way.

Now guess what:
java.rmi.RemoteException is a checked
exception. Bad choice for an exception which every method might
raise. Because it means that every method has to specify
then. Ok, from a documentation point of view. But it also means that
remote objects can't implement the java.lang.Comparable<T>
interface. Or be a java.beans.PropertyChangeListener. A
remote object can't even implement the equals method.


All the cool design patterns and
conventions I learned for the “Sun Certified Java Programmer”
down the drain. How is one supposed to create elegant code “to
be maintained by a junior programmer”
under theses
conditions?


Ahh, yes, socket communication was
the alternative. But that does not sound like elegant code or junior
programmer compatible either.


Keine Kommentare:

Kommentar veröffentlichen