Java Exception Handling
Definitions
- Contingency: An expected condition demanding an alternative response from a method that can be expressed in terms of the method's intended purpose. The caller of the method expects these kinds of conditions and has a strategy for coping with them.
- Fault: An unplanned condition that prevents a method from achieving its intended purpose that cannot be described without reference to the method's internal implementation.
Java Mapping
|
Contingency |
Fault |
Is considered to be |
A part of the design |
A nasty surprise |
Is expected to happen |
Regularly but rarely |
Never |
Who cares about it |
The upstream code that invokes the method |
The people who need to fix the problem |
Examples |
Alternative return modes |
Programming bugs, hardware malfunctions, configuration mistakes, missing files, unavailable servers |
Best Mapping |
A checked exception |
An unchecked exception |
Fault Handling Framework
A successful fault handling framework has to accomplish four goals:
- Minimize code clutter (avoiding Checked Exceptions)
- Capture and preserve diagnostics (Through Exception Chaining)
- Alert the right person (Generate error pages, SOAP Faults or logging messages)
- Exit the activity gracefully
Fault Barrier Pattern
In the fault barrier pattern, any application component can throw a fault exception, but only the component acting as the "fault barrier" catches them. Adopting this pattern eliminates much of the intricate code that developers insert locally to deal with faults. The fault barrier resides logically toward the top of the call stack where it stops the upward propagation of an exception before default action is triggered. Default action means different things depending on the application type. For a stand-alone Java application, it means that the active thread is terminated. For a Web application hosted by an application server, it means that the application server sends an unfriendly (and embarrassing) response to the browser.
References
--
LorenzoDini - 14 Aug 2009