-
-
Save ranarula/1a2123354025e2287fcbbdf80c61bb37 to your computer and use it in GitHub Desktop.
Hide checked exceptions in Java Streams
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 static <T> T uncheckCall(Callable<T> callable) { | |
| try { return callable.call(); } | |
| catch (Exception e) { return sneakyThrow(e); } | |
| } | |
| public static void uncheckRun(RunnableExc r) { | |
| try { r.run(); } catch (Exception e) { sneakyThrow(e); } | |
| } | |
| public interface RunnableExc { void run() throws Exception; } | |
| public static <T> T sneakyThrow(Throwable e) { | |
| return Util.<RuntimeException, T>sneakyThrow0(e); | |
| } | |
| private static <E extends Throwable, T> T sneakyThrow0(Throwable t) throws E { throw (E)t; } | |
| // Usage sample | |
| //return s.filter(a -> uncheckCall(a::isActive)) | |
| // .map(Account::getNumber) | |
| // .collect(toSet()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment