Remote Exceptions and the LSP

Why did I rabbit on about the Liskov Substitution Principle anyway? Because the infamous RemoteException is an example of the issue.

In a “location transparent” mode of working, you don’t know if the object you are talking to is collocated in the same JVM, or halfway around the world. You can literally substitue the remote object for the local one; the LSP at work.

However, when you’re talking to a remote object, you need to deal with the problem that distributed applications aren’t very reliable. Thus, the java.rmi.RemoteException was born.

But a RemoteException can’t be a runtime exception; otherwise when you drop the remote object in, you’d risk breaking the client code (which isn’t expecting runtime exceptions to propogate out). Thus, it has to be checked.

If it’s checked, however, the client code must always deal with RemoteExceptions. Only if the client says “Okay, I’ll just use a local version” can you ignore them.

This is a good example of the LSP at work; if you want the ability to substitue a remote object for a local one, you need to have an interface contract which permits the substitution. If you don’t, then you out of luck.

(Location transparency is an odd concept, BTW; many people interpret it to mean that remote objects are treated the same as local, which is why they complain about the RemoteExceptions. The correct intepretation should be that local objects get treated as remote)

Author: Robert Watkins

My name is Robert Watkins. I am a software developer and have been for over 20 years now. I currently work for people, but my opinions here are in no way endorsed by them (which is cool; their opinions aren’t endorsed by me either). My main professional interests are in Java development, using Agile methods, with a historical focus on building web based applications. I’m also a Mac-fan and love my iPhone, which I’m currently learning how to code for. I live and work in Brisbane, Australia, but I grew up in the Northern Territory, and still find Brisbane too cold (after 22 years here). I’m married, with two children and one cat. My politics are socialist in tendency, my religious affiliation is atheist (aka “none of the above”), my attitude is condescending and my moral standing is lying down.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: