Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created April 7, 2016 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tarrasch/a817b1449bfcc5e3653b0bdbccdfec62 to your computer and use it in GitHub Desktop.
Save Tarrasch/a817b1449bfcc5e3653b0bdbccdfec62 to your computer and use it in GitHub Desktop.
Playing around with muting exception in Java 8
public class Helpers {
public static <T> T getWithRTE(ExceptionSupplier<T> exceptionThrower) {
try {
return exceptionThrower.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
//int num = ThrowingSupplier.unchecked(Helpers::readIntFromFile).get();
int num = getWithRTE(() -> readIntFromFile());
num = getWithRTE(Helpers::readIntFromFile);
}
private static Integer readIntFromFile() throws Exception {
return 4;
}
private static Integer launchMissiles() throws Exception {
return 5;
}
private static Integer narrowerException() throws ArithmeticException {
return 6;
}
@FunctionalInterface
public interface ExceptionSupplier<T> {
T get() throws Exception;
}
public static void other_main(String[] args) {
ExceptionSupplier<Integer> supplier = () -> launchMissiles();
System.out.println(getWithRTE(supplier));
System.out.println(getWithRTE(() -> narrowerException()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment