Core Java
The Mute Design Pattern
Have you been writing a lot of code following the Mute-Design-Pattern™ lately? E.g.
try { complex(); logic(); here(); } catch (Exception ignore) { // Will never happen hehe System.exit(-1); }
There’s an easier way with Java 8!
Just add this very useful tool to your Utilities or Helper class:
public class Helper { // 18395 lines of other code here @FunctionalInterface interface CheckedRunnable { void run() throws Throwable; } public static void mute(CheckedRunnable r) { try { r.run(); } catch (Throwable ignore) { // OK, better stay safe ignore.printStackTrace(); } } // 37831 lines of other code here }
Now you can wrap all your logic in this nice little wrapper:
mute(() -> { complex(); logic(); here(); });
Done!
Even better, in some cases, you can use method references
try (Connection con = ...; PreparedStatement stmt = ...) { mute(stmt::executeUpdate); }
Reference: | The Mute Design Pattern from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog. |