Error Hiding - Manifestations in Languages That Support Checked Exceptions

Manifestations in Languages That Support Checked Exceptions

Error hiding is one of the more common anti-patterns to encounter in languages that support the paradigm of checked exceptions including Java. This type of exception forces the programmer to handle the exception even if the programmer has no means to effectively do so. The problem becomes more egregious in large or complex systems. For example, a programmer implementing function A may require the service of another component B. Component B may then in turn delegate to component C to fulfill that request. If component C encounters a failure during that service it may indicate this by throwing a checked exception to component B. Component B after recovering from the error but unable to fulfill the request from Function A may indicate this by re-throwing the error from C. Function A, having no recourse to handle an error from component C has three options:

  1. Re-throw the exception
  2. Catch the exception, log it and re-throw it
  3. Catch the exception but do nothing meaningful with it.

A novice programmer may choose the third option and hide the error (also referred to as exception swallowing). The consequence of swallowing the exception is that the system may be put into an unstable state but its users (both human and machine) will remain unaware that a critical failure has occurred. These type of errors when finally discovered are sometimes near impossible to debug because when it does manifests as a noticeable error it is sometimes much later in the process from where the error actually occurred.

Given a large multi-layered application with a high amount of inter-object communication checked exceptions may end up being passed through multiple layers before finally reaching a point where they can be properly handled and reported the end user. In addition to being subject to potential hiding issues at each layer, domain models also become polluted with excessive coupling.

For this reason, version 1.4 and later of Java support exception chaining for all exceptions.

Read more about this topic:  Error Hiding

Famous quotes containing the words languages, support, checked and/or exceptions:

    The less sophisticated of my forbears avoided foreigners at all costs, for the very good reason that, in their circles, speaking in tongues was commonly a prelude to snake handling. The more tolerant among us regarded foreign languages as a kind of speech impediment that could be overcome by willpower.
    Barbara Ehrenreich (b. 1941)

    Because the young child feels with such intensity, he experiences sorrows that seem inconsolable and losses that feel unbearable. A precious toy gets broken or a good-bye cannot be endured. When this happens, words like “sad” or “disappointed” seem a travesty because they cannot possibly capture the enormity of the child’s loss. He needs a loving adult presence to support him in his pain but he does not want to be talked out of it.
    Alicia F. Lieberman (20th century)

    Be checked for silence,
    But never taxed for speech.
    William Shakespeare (1564–1616)

    Every declaration of love contains an unstated list of exceptions and demands.
    Mason Cooley (b. 1927)