Skip to content

Instantly share code, notes, and snippets.

@dimaKudr
Created October 18, 2015 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dimaKudr/b31f25fc64d0759b573e to your computer and use it in GitHub Desktop.
Save dimaKudr/b31f25fc64d0759b573e to your computer and use it in GitHub Desktop.
Hide checked exceptions in Java Streams
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