自定义函数式@FunctionalInterface异常接口
1、添加注解
/** * 抛出异常函数接口 * * @author liunancun * @date 2021/2/5 */ @FunctionalInterface public interface ThrowExceptionSupplier<T> { /** * 返回一个结果 * * @return * @throws Exception */ T get() throws Exception; }
2、接口作为参数使用
/** * 捕获异常 * * @param action * @param <R> * @return */ public static <R> R tryCatch(ThrowExceptionSupplier<R> action) { try { return action.get(); } catch (BaseException e) { throw e; } catch (Exception e) { throw new BaseException(e.getMessage(), e); } }