1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
@FunctionalInterface
interface ExceptionFunction<T, R, E extends Exception> {
R apply(T t) throws E;
}
/**
* 包装异常
*/
public static <T, R, E extends Exception> Function<T, R> wrap(ExceptionFunction<T, R, E> f) throws E {
return t -> {
try {
return f.apply(t);
} catch (Exception e) {
throwUnCheckException(e);
return null;
}
};
}
/**
* 利用泛型擦除机制将unCheck——>Exception
*/
public static <E extends Exception> void throwUnCheckException(Exception e) throws E {
throw (E) e;
}
|
近期评论