项目异常处理方案

异常处理

项目异常处理方案

异常分类

业务异常

​ 规范的用户行为产生的异常

​ 不规范的用户行为操作产生的异常

系统异常

​ 项目运行过程中可预计且无法避免的异常

其他异常

​ 编程人员未预期到的异常

异常处理方案

业务异常

​ 发送对应的消息传递给用户,提醒规范操作

系统异常

​ 发送固定消息传递给用户,安抚用户

​ 发送特定的消息给运维人员,提醒维护

​ 记录日志

其他异常

​ 发送固定消息传递给用户,安抚用户

​ 发送特定消息给编程人员,提醒维护

​ 纳入预期范围内

​ 记录日志

自定义异常

业务异常
package com.test.springmvc.exception;

/**
 * @author zhangzengxiu
 * @date 2021/11/29
 */
public class BussinessException extends RuntimeException {

    private Integer code;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public BussinessException(Integer code) {
        this.code = code;
    }

    public BussinessException(String message,Integer code) {
        super(message);
        this.code = code;
    }

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

    public BussinessException(Throwable cause,Integer code) {
        super(cause);
        this.code = code;
    }

    public BussinessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace,Integer code) {
        super(message, cause, enableSuppression, writableStackTrace);
        this.code = code;
    }
}
系统异常
package com.test.springmvc.exception;

/**
 * @author zhangzengxiu
 * @date 2021/11/29
 */
public class SystemException extends RuntimeException {
    public SystemException() {
    }

    public SystemException(String message) {
        super(message);
    }

    public SystemException(String message, Throwable cause) {
        super(message, cause);
    }

    public SystemException(Throwable cause) {
        super(cause);
    }

    public SystemException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}
异常处理器:
package com.test.springmvc.exception;

import com.test.springmvc.result.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 异常处理器
 *
 * @author zhangzengxiu
 * @date 2021/11/29
 */
@ControllerAdvice
@Component
public class ExceptionAdvice {

    @ResponseBody
    @ExceptionHandler(BussinessException.class)
    public Result doBussinessException(BussinessException e) {
        return new Result(e.getCode(), e.getMessage());
    }

}
Controller:
	@GetMapping("/findById/{id}")
    public Result<User> findById(@PathVariable("id") int id) {
        User user = userService.findById(id);
        if (user == null) {
            throw new BussinessException(Status.GET_ERR.getCode());
        }
        return new Result(Status.GET_OK.getCode(), Status.GET_OK.getMessage(), user);
    }

实际企业开发过程中,Controller不会再抛异常出去,会在这层做try{}catch(){},抓住然后进行统一处理
参考博客
如下:

  	@GetMapping("/findById/{id}")
    public Result<User> findById(@PathVariable("id") int id) {
        try {
            User user = userService.findById(id);
            if (user == null) {
                return new Result(Status.GET_ERR.getCode(), Status.GET_ERR.getMessage());
            }
            return new Result(Status.GET_OK.getCode(), Status.GET_OK.getMessage(), user);
        } catch (Exception e) {
            return new Result(Status.GET_ERR.getCode(), Status.GET_ERR.getMessage());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值