Use template for transactions
try-catchblocks are particularly common when using APIs which give an important role to checked exceptions (such as
SQLExceptionin JDBC). When using such an API, many published examples simply repeat the same
try-catchcode structure whenever necessary. However, it's simple to eliminate such code repetition using the template method pattern. The idea is to define the structure of the
try-catchblock in one place, in an abstract base class (ABC). Such an ABC is then extended whenever that particular
try-catchblock is needed. The concrete implementation of such an ABC will often have simple, "straight line" code.
An extended example of this technique is given in the Use
template for transactions topic.