自定义函数式@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);
        }
    }

 

posted @ 2022-03-24 16:43  这很周锐  阅读(117)  评论(0编辑  收藏  举报