Created
January 15, 2014 14:38
-
-
Save thermz/8437412 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Interface block | |
public interface Consumer<T> { | |
public T unchecked() throws Exception; | |
} | |
//unchecked static method that receive Consumer interface as a block | |
public static <T> T unchecked(Consumer<T> consumer){ | |
try { | |
consumer.unchecked(); | |
} catch(Exception e){ | |
throw new RuntimeException(e); | |
} | |
} | |
// In your code, with import static | |
unchecked(new Consumer<Something>(){ | |
public Something unchecked() throws Exception { | |
// your unchecked code :-) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A very unreadable way to consume Checked Exception in Java 6,
Bad: Very difficult to read
Good: less cyclomatic complexity than try { } catch(Exception e) { throw new RuntimeException(e); } repeated every time you want to "uncheck" your code.