Skip to content

Instantly share code, notes, and snippets.

@ranarula
Forked from dimaKudr/JavaStreamExceptions.java
Created October 9, 2018 22:12
Show Gist options
  • Select an option

  • Save ranarula/1a2123354025e2287fcbbdf80c61bb37 to your computer and use it in GitHub Desktop.

Select an option

Save ranarula/1a2123354025e2287fcbbdf80c61bb37 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