20
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
* THE SOFTWARE.
22
22
*/
23
+
23
24
package com .iluwatar .mute ;
24
25
25
26
import static org .mockito .Mockito .doThrow ;
28
29
import java .io .ByteArrayOutputStream ;
29
30
import java .sql .Connection ;
30
31
import java .sql .SQLException ;
32
+ import java .sql .Statement ;
31
33
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
+ */
32
51
public class App {
33
52
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
+
36
61
useOfLoggedMute ();
37
-
62
+
38
63
useOfMute ();
39
64
}
40
65
41
66
/*
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.
45
71
*/
46
72
private static void useOfMute () {
47
73
ByteArrayOutputStream out = new ByteArrayOutputStream ();
48
74
Mute .mute (() -> out .write ("Hello" .getBytes ()));
49
75
}
50
76
51
- private static void useOfLoggedMute () {
77
+ private static void useOfLoggedMute () throws SQLException {
52
78
Connection connection = null ;
53
79
try {
54
80
connection = openConnection ();
55
81
readStuff (connection );
56
- } catch (SQLException ex ) {
57
- ex .printStackTrace ();
58
82
} finally {
59
83
closeConnection (connection );
60
84
}
@@ -64,14 +88,12 @@ private static void useOfLoggedMute() {
64
88
* All we can do while failed close of connection is to log it.
65
89
*/
66
90
private static void closeConnection (Connection connection ) {
67
- if (connection != null ) {
68
- Mute .loggedMute (() -> connection .close ());
69
- }
91
+ Mute .loggedMute (() -> connection .close ());
70
92
}
71
93
72
94
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" );
75
97
}
76
98
}
77
99
0 commit comments