Netflix/curator — Issue #13: Use more specific exception types
Author: cb372
Created: 2011-12-19 03:17:22+00:00
State: open

This is half question, half feature request.

Is there a particular reason that the `forPath()` methods throw `Exception`? If not, I think it would be more useful to throw a more specific exception, e.g. `CuratorException`, `ZooKeeperException` or whatever.

In my application I have a lot of places where I mix ZK-related code and app-specific code and surround the whole thing with one try-catch, e.g. (using the standard Apache ZK client):

try {
if (zk.exists(path, false) != null) {
// deserialize the zNode's data, which could throw a MyDeserializationFailedException
} else {
// zNode should exist, but does not, so throw a MyDataNotFoundException
}
} catch (KeeperException e) {
throw new ZooKeeperIsBorkedException(e)
}

This way, I can handle the 3 different types of exception at a higher level, easily differentiating between the various error cases.

But with Curator, I have to either completely rewrite this code or add ugly rethrows like this:

try {
if (curator.checkExists().forPath(path) != null) {
// deserialize the zNode's data, which could throw a MyDeserializationFailedException
} else {
// zNode should exist, but does not, so throw a MyDataNotFoundException
}
} catch (MyDeserializationFailedException | MyDataNotFoundException e) {
throw e;
} catch (Exception e) {
throw new ZooKeeperIsBorkedException(e)
}