异常接口
public class Try {
public static <T> Consumer<? super T> of(UncheckedConsumer<T> action) {
Objects.requireNonNull(action);
return t -> {
try {
action.accept(t);
} catch (Exception e) {
throw new RuntimeException("Error: " + e.getMessage());
}
};
}
@FunctionalInterface
public interface UncheckedConsumer<T> {
void accept(T t) throws Exception;
}
}
演示 by zero
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 0);
integers.forEach( Try.of(i -> System.out.println(50 / i)));
标签:Exception,accept,java8,UncheckedConsumer,action,异常,public,Lambda
From: https://www.cnblogs.com/guanchaoguo/p/18120667