Skip to content

Instantly share code, notes, and snippets.

@thermz
Created January 15, 2014 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thermz/8437412 to your computer and use it in GitHub Desktop.
Save thermz/8437412 to your computer and use it in GitHub Desktop.
//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
Copy link
Author

thermz 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