Skip to content

Instantly share code, notes, and snippets.

@shawnz
Last active September 16, 2020 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawnz/5e9a0d344a6a693b46c662c5c8124596 to your computer and use it in GitHub Desktop.
Save shawnz/5e9a0d344a6a693b46c662c5c8124596 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.sql.SQLException;
public class Test {
public static String a(String x) throws IOException {
return x;
}
public static String b(String x) throws SQLException {
return x;
}
public static <X, Y, Z, T1 extends Throwable, T2 extends Throwable> FuncThrowingTwoExceptions<X, Z, T1, T2> compose(
FuncThrowingOneException<X, Y, T1> a, FuncThrowingOneException<Y, Z, T2> b) {
return t -> b.apply(a.apply(t));
}
public static String c(String x) throws IOException, SQLException {
return compose(Test::a, Test::b).apply(x);
}
}
interface FuncThrowingOneException<X, Y, T extends Throwable> {
Y apply(X x) throws T;
}
interface FuncThrowingTwoExceptions<X, Y, T1 extends Throwable, T2 extends Throwable> {
Y apply(X x) throws T1, T2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment