Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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 :-)
}
});
@thermz

This comment has been minimized.

Copy link
Owner Author

commented Jan 15, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.