首页 > 其他分享 >try catch异常捕获工具类

try catch异常捕获工具类

时间:2023-03-13 21:22:56浏览次数:28  
标签:errLogMsg return log TryCatchUtil 捕获 param try catch public

异常捕获工具类

package com.example.multiThreadTransaction_demo.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 异常捕获工具类
 * <p>
 * 不支持finally
 * 异常信息提示日志必须打印
 *
 * @author: shf
 * @date: 2023年03月09日 12:59
 */
@Slf4j
public class TryCatchUtil<T, R> {

    /**
     * 入参
     */
    private T param;

    /**
     * 返回值(可自定义设置默认值)
     */
    private R result;

    /**
     * 异常提示日志打印(可自定义设置)
     */
    private String errLogMsg;

    public TryCatchUtil(T param, R result, String errLogMsg) {
        this.param = param;
        this.result = result;
        this.errLogMsg = errLogMsg;
    }

    /**
     * 没有默认值抛出异常将返回null,注意NPE处理
     * @param errLogMsg 异常日志提示
     * @return
     */
    public static <T, R> TryCatchUtil<T, R> of(String errLogMsg) {
        return of(null, errLogMsg);
    }

    public static <T, R> TryCatchUtil<T, R> of(T param, String errLogMsg) {
        return of(param, null, errLogMsg);
    }

    public static <T, R> TryCatchUtil<T, R> of(T param, R defaultVal, String errLogMsg) {
        return new TryCatchUtil<>(param, defaultVal, errLogMsg);
    }

    /**
     * 有入参 有返回值
     *
     * @param function 函数式接口入参
     * @return R
     */
    public R apply(Function<T, R> function) {
        try {
            result = function.apply(param);
        } catch (Throwable e) {
            log.error(StringUtils.isNotBlank(errLogMsg) ? errLogMsg : e.getMessage(), e);
        }
        return result;
    }

    /**
     * 有入参 无返回值
     *
     * @param consumer 函数式接口入参
     */
    public void accept(Consumer<T> consumer) {
        try {
            consumer.accept(param);
        } catch (Throwable e) {
            log.error(StringUtils.isNotBlank(errLogMsg) ? errLogMsg : e.getMessage(), e);
        }
    }

    /**
     * 无入参  有返回值
     *
     * @param supplier 函数式接口入参
     * @return R
     */
    public R get(Supplier<R> supplier) {
        try {
            return supplier.get();
        } catch (Throwable e) {
            log.error(StringUtils.isNotBlank(errLogMsg) ? errLogMsg : e.getMessage(), e);
        }
        return result;
    }

    /**
     * 无入参  无返回值
     *
     * @param exceptionFunction 函数式接口入参
     */
    public void handle(ExceptionFunction exceptionFunction) {
        try {
            exceptionFunction.applay();
        } catch (Throwable e) {
            log.error(StringUtils.isNotBlank(errLogMsg) ? errLogMsg : e.getMessage(), e);
        }
    }

    /**
     * 自定义异常Function
     */
    @FunctionalInterface
    public interface ExceptionFunction {

        void applay();
    }

    /**
     * 测试用例
     *
     * @param isThrow 是否抛出异常
     * @return int
     */
    private static Integer applyTest(Boolean isThrow) {
        log.info("---applyTest---");
        if (isThrow) {
            int a = 1 / 0;
        }
        return 1;
    }

    /**
     * 测试用例
     *
     * @return int
     */
    private static Integer getTest() {
        log.info("---getTest---");
        return 2;
    }

    /**
     * 测试用例
     *
     * @param isThrow 是否抛出异常
     */
    private static void acceptTest(Boolean isThrow) {
        log.info("---acceptTest---");
        if (isThrow) {
            int a = 1 / 0;
        }
    }

    /**
     * 测试用例
     */
    private static void handleTest() {
        log.info("---handleTest---");
    }

    public static void main(String[] args) {
        Integer apply1 = TryCatchUtil.<Boolean, Integer>of(true, 100, "测试异常").apply(TryCatchUtil::applyTest);
        Integer apply2 = TryCatchUtil.<Boolean, Integer>of(true, "测试异常").apply(TryCatchUtil::applyTest);
        Integer get2 = TryCatchUtil.<Boolean, Integer>of("测试异常").get(TryCatchUtil::getTest);
        TryCatchUtil.of(false, "测试异常").accept(TryCatchUtil::acceptTest);
        TryCatchUtil.of("测试异常").handle(TryCatchUtil::handleTest);

        log.info("apply1=[{}];apply2=[{}];get2=[{}]", apply1, apply2, get2);
    }
}

 

标签:errLogMsg,return,log,TryCatchUtil,捕获,param,try,catch,public
From: https://www.cnblogs.com/better-farther-world2099/p/17212923.html

相关文章