Database Reference
In-Depth Information
modes and what we can do to correct them so that our programs are resilient in the face of
failure.
Every ZooKeeper operation in the Java API declares two types of exception in its
throws
clause:
InterruptedException
and
KeeperException
.
InterruptedException
An
InterruptedException
is thrown if the operation is interrupted. There is a
standard Java mechanism for canceling blocking methods, which is to call
inter-
rupt()
on the thread from which the blocking method was called. A successful cancel-
lation will result in an
InterruptedException
. ZooKeeper adheres to this standard,
so you can cancel a ZooKeeper operation in this way. Classes or libraries that use
ZooKeeper usually should propagate the
InterruptedException
so that their cli-
An
InterruptedException
does not indicate a failure, but rather that the operation
has been canceled, so in the configuration application example it is appropriate to propag-
ate the exception, causing the application to terminate.
KeeperException
A
KeeperException
is thrown if the ZooKeeper server signals an error or if there is a
communication problem with the server. For different error cases, there are various sub-
classes of
KeeperException
. For example,
KeeperExcep-
tion.NoNodeException
is a subclass of
KeeperException
that is thrown if you
try to perform an operation on a znode that doesn't exist.
Every subclass of
KeeperException
has a corresponding code with information about
the type of error. For example, for
KeeperException.NoNodeException
, the
code is
KeeperException.Code.NONODE
(an enum value).
There are two ways, then, to handle
KeeperException
: either catch
KeeperExcep-
tion
and test its code to determine what remedying action to take, or catch the equival-
ent
KeeperException
subclasses and perform the appropriate action in each catch
block.
KeeperException
s fall into three broad categories.
State exceptions
A state exception occurs when the operation fails because it cannot be applied to the
znode tree. State exceptions usually happen because another process is mutating a znode