Java Exception Handling
Java exception handling is very successful, and does exactly what I want as a system designer.<p>The article objects to the fact that applications handle many exceptions by logging them and giving up. What&#x27;s wrong with that? That&#x27;s perfect. For the class of exceptions that I <i>can</i> just log and give up, that&#x27;s often what I want to do. If I had an open TCP socket to a remote host, then I can&#x27;t &quot;recover&quot; from an exception indicating that the connection was broken. Instead, I just want to tear down everything related to that connection and carry on.<p>Exceptions can rarely be recovered from at the level of abstraction where the exception occurred. For example, I can&#x27;t repair a socket that&#x27;s been broken. I cannot retry the request at that level - it might not be idempotent; the socket processing code can&#x27;t know. The connection pool doesn&#x27;t know. The HTTP client might or might not know. However, I <i>can</i> recover from the exception at a higher level -- at the level of the code that understands what the task is, it might retry the top level processing job, which might invoke a call on a service client, which will invoke a call on an HTTP client, which will invoke a call on an HTTP connection pool to open a new TCP socket as needed. This is exactly the pattern that we see commonly in Java applications: a higher level module calls a lower level one, and then variously logs exceptions and tries again (possibly with exponential backoff over multiple attempts), or logs them and give up, or does something else.<p>You don&#x27;t really &quot;repair&quot; or &quot;recover&quot; from exceptions. You mitigate them by handling them and carrying on with the job. To achieve this, you unwind up to the level where it makes sense to retry or abort a task. When you retry, you retry from the right level of abstraction needed to start over.<p>The extremely valuable property that exceptions have, that they give you, is confidence about what specifically went wrong. They constrain it. If I try to interact with a socket and I get an IO exception, then I know the error is constrained specifically to that socket. The rest of the program is working fine, and I can either try to recover from that error (sometimes possible), or close the connection and open a new one, or give up processing the task.<p>Exceptions are also valuable because you don&#x27;t have to add error-handling logic at each layer in an application. Many layers can be ignorant or agnostic of the exceptions that will propagate through them, and that&#x27;s a good thing because it reduces coupling. These layers might be inserted between the high-level logic that understands task processing, and that will make the judgment call about retry&#x2F;backoff&#x2F;give up, and the specific code that can fail and throw an exception. I don&#x27;t want all layers of code to <i>have</i> to care about exceptions that might pass through them; they can&#x27;t and shouldn&#x27;t do anything about it.<p>It is valuable to be able to build abstractions and build code that exceptions <i>pass through</i>, without handling them. You might add logic to handle them later, or you might do so at a higher level, or a lower level.