Created
April 7, 2016 03:22
-
-
Save Tarrasch/a817b1449bfcc5e3653b0bdbccdfec62 to your computer and use it in GitHub Desktop.
Playing around with muting exception in Java 8
This file contains hidden or 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
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