2020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121 * THE SOFTWARE.
2222 */
23+
2324package com .iluwatar .mute ;
2425
2526import static org .mockito .Mockito .doThrow ;
2829import java .io .ByteArrayOutputStream ;
2930import java .sql .Connection ;
3031import java .sql .SQLException ;
32+ import java .sql .Statement ;
3133
34+ /**
35+ * Mute pattern is utilized when we need to suppress an exception due to an API flaw or in
36+ * situation when all we can do to handle the exception is to log it.
37+ * This pattern should not be used everywhere. It is very important to logically handle the
38+ * exceptions in a system, but some situations like the ones described above require this pattern,
39+ * so that we don't need to repeat
40+ * <pre>
41+ * <code>
42+ * try {
43+ * // code that may throwing exception we need to ignore or may never be thrown
44+ * } catch (Exception ex) {
45+ * // ignore by logging or throw error if unexpected exception occurs
46+ * }
47+ * </code>
48+ * </pre> every time we need to ignore an exception.
49+ *
50+ */
3251public class App {
3352
34- public static void main (String [] args ) {
35-
53+ /**
54+ * Program entry point.
55+ *
56+ * @param args command line args.
57+ * @throws Exception if any exception occurs
58+ */
59+ public static void main (String [] args ) throws Exception {
60+
3661 useOfLoggedMute ();
37-
62+
3863 useOfMute ();
3964 }
4065
4166 /*
42- * Typically used when the API declares some exception but cannot do so. Usually a signature mistake.
43- * In this example out is not supposed to throw exception as it is a ByteArrayOutputStream. So we
44- * utilize mute, which will throw AssertionError if unexpected exception occurs.
67+ * Typically used when the API declares some exception but cannot do so. Usually a
68+ * signature mistake.In this example out is not supposed to throw exception as it is a
69+ * ByteArrayOutputStream. So we utilize mute, which will throw AssertionError if unexpected
70+ * exception occurs.
4571 */
4672 private static void useOfMute () {
4773 ByteArrayOutputStream out = new ByteArrayOutputStream ();
4874 Mute .mute (() -> out .write ("Hello" .getBytes ()));
4975 }
5076
51- private static void useOfLoggedMute () {
77+ private static void useOfLoggedMute () throws SQLException {
5278 Connection connection = null ;
5379 try {
5480 connection = openConnection ();
5581 readStuff (connection );
56- } catch (SQLException ex ) {
57- ex .printStackTrace ();
5882 } finally {
5983 closeConnection (connection );
6084 }
@@ -64,14 +88,12 @@ private static void useOfLoggedMute() {
6488 * All we can do while failed close of connection is to log it.
6589 */
6690 private static void closeConnection (Connection connection ) {
67- if (connection != null ) {
68- Mute .loggedMute (() -> connection .close ());
69- }
91+ Mute .loggedMute (() -> connection .close ());
7092 }
7193
7294 private static void readStuff (Connection connection ) throws SQLException {
73- if ( connection != null ) {
74- connection . createStatement ( );
95+ try ( Statement statement = connection . createStatement () ) {
96+ System . out . println ( "Read data from statement" );
7597 }
7698 }
7799
0 commit comments