Java自定义异常类统一处理异常

当程序发生异常时,会返回一大堆不友好的内容,非常不美观!

我们在写代码的时候,对异常处理一般是try catch或者抛出异常throws Exception。

try catch大家都知道,代码中大量的try catch会占用内存影响性能,而且需要在每个方法的代码块加上try catch,非常的繁琐;throws Exception也一样需要加在每个方法后面。

那么怎样去统一处理异常呢?

一、首先我们创建一个class类,继承Exception或者他的子类:

/**
 * 自定义异常类
 */
public class GlobalExecption extends RuntimeException {

    private static final long serialVersionUID = 4453214753962022203L;
    private Integer code;
    private String msg;

    public GlobalExecption() {}

    public GlobalExecption(int code, String msg) {
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    public GlobalExecption(String msg) {
        super(msg);
        this.msg = msg;
    }

    public GlobalExecption(String msg, Throwable cause) {
        super(msg, cause);
        this.msg = msg;
    }

    public GlobalExecption(Integer code, String msg, Throwable cause) {
        super(msg, cause);
        this.code = code;
        this.msg = msg;
    }

    public static GlobalExecption paramException(String message) {
        GlobalExecption baseExecption = new GlobalExecption(HttpCode.CODE_400, message);
        return baseExecption;
    }

    public static GlobalExecption serverErrException(String message) {
        return new GlobalExecption(HttpCode.CODE_500, message);
    }

    public static GlobalExecption serverErrException(String message, Exception e) {
        return new GlobalExecption(HttpCode.CODE_500, message, e);
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }
}

二、创建一个Handler(非常关键,在这里可以返回自定义消息体给前端页面):

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常统一处理
 */
@RestControllerAdvice
public class DefaultExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    public Result serverError(Exception e) {
        String errMsg = "";
        if (e instanceof NullPointerException) {
            errMsg = "发生空指针异常";
        } else if (e instanceof RuntimeException) {
            errMsg = "发生运行时异常";
        } else {
            errMsg = "发生未知异常";
        }
        logger.error("############" + errMsg + "############", e);
        return ResultTemplate.error(HttpCode.CODE_500, errMsg);
    }

    @ExceptionHandler(value = GlobalExecption.class)
    public Result<Object> paramError(GlobalExecption e) {
        logger.info("############" + e.getMsg() + "############");
        return ResultTemplate.error(e.getCode(), e.getMsg());
    }

    /**
     * 处理所有参数校验失败的异常(MethodArgumentNotValidException异常)
     * 设置响应状态码为400
     * @param ex
     * @return
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ResultEntity handleBindGetException(MethodArgumentNotValidException ex) {
        // 获取所有异常
        /*List<String> errors = ex.getBindingResult()
                .getFieldErrors()
                .stream()
                .map(x -> x.getDefaultMessage())
                .collect(Collectors.toList());
        String errMsg = String.join(",", errors);*/
        // 校验参数,拿到@NotNull的message内容
        String errMsg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return ResultTemplate.error(HttpCode.CODE_400, errMsg);
    }
}

再贴出我的ResultTemplate代码:

import java.io.Serializable;

/**
 * 自定义返回页面结果
 */
public class ResultTemplate<T> implements Serializable {

    private static final long serialVersionUID = -669633312320552296L;

    /**
     * 返回成功,用于新增、编辑、删除操作
     * @param msg
     * @return
     */
    public static <T> Result<T> success(String msg) {
        Result <T>  result = new Result<T>();
        result.setSuccess(true);
        result.setCode(HttpCode.CODE_200);
        result.setMessage(msg);
        return result;
    }

    /**
     * 返回成功,用于查询
     * @param data
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Result<T> successData(T data, String msg) {
        Result result = new Result<>();
        result.setSuccess(true);
        result.setCode(HttpCode.CODE_200);
        result.setMessage(msg);
        result.setData(data);
        return result;
    }


    /**
     * 返回成功,用于新增、编辑、删除操作
     * @param msg
     * @return
     */
    public static <T> Result<T> fail(String msg) {
        Result <T>  result = new Result<T>();
        result.setSuccess(false);
        result.setCode(HttpCode.CODE_500);
        result.setMessage(msg);
        return result;
    }

    public static <T> Result<T> error(Integer code, String msg) {
        Result <T>  result = new Result<T>();
        result.setSuccess(false);
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
}

在这里我们千万不要用e.printStackTrace();去打印异常到控制台(e.printStackTrace()产生错误堆栈字符串到字符串池内存空间,当内存空间被占满,造成服务应用宕机就杯具了);

三、接下来在我们代码里抛出自定义异常:

@PostMapping("/queryAll")
@LogByMethod(remark = "查询所有人", editType = EditTypeEnum.QUERY)
public Result queryAll(@RequestBody Map<String, String> map) {
    if (true) {
        throw GlobalExecption.serverErrException("测试异常处理");
    }
    threadService.queryAll();
    return ResultTemplate.success(ComMsgContent.QUERY_SUCCESS_MSG);
}

在postman调用,测试下返回什么结果:

然后再看看控制台的输出:

ok,到这里我们就完成了自定义异常统一处理!

  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值