设为首页 加入收藏

TOP

统一异常处理
2023-07-25 21:24:12 】 浏览:33
Tags:常处理

Spring Boot中的统一异常处理

  • Result为封装传递给前端的包装类
  • 全局异常处理
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: KeYu
 * @Package: com.feiyu.common.exception
 * @Date: 2023/05/17/9:05
 * @说明:统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常处理,执行方法
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)   //这里是要捕获的异常类Exception
    @ResponseBody                        //这里返回数据
    public Result error(Exception e){
        e.printStackTrace();             //输出错误
        return Result.fail().massage("执行了全局异常处理。。。。。");
    }
}
  • 特定异常处理
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: KeYu
 * @Package: com.feiyu.common.exception
 * @Date: 2023/05/17/9:05
 * @说明:统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 特定异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ArithmeticException.class)    //这里是要捕获的异常类ArithmeticException
    @ResponseBody
    public Result error(ArithmeticException e){
        e.printStackTrace();
        return Result.fail().massage("执行了特定异常处理。。。。。");
    }
}
  • 自定义异常处理
  1. 创建异常类,继承RuntimeException
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: KeYu
 * @Package: com.feiyu.common.exception
 * @Date: 2023/05/17/9:23
 * @说明:自定义异常类
 */
@Data
public class FeiYuException extends RuntimeException{
    private Integer code;
    private String msg;

    public FeiYuException(Integer code,String msg){
        super(msg);
        this.code = code;
        this.msg = msg;
    }

    /**
     * 接收枚举类对象
     */
    public FeiYuException(ResultCodeEnum resultCodeEnum){
        super(resultCodeEnum.getMessage());
        this.code = resultCodeEnum.getCode();
        this.msg=resultCodeEnum.getMessage();
    }
}
  1. 在异常的地方,手动添加异常
@ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll(){
        try {
            int a = 10/0;
        }catch (Exception e){
            //抛出自定义异常
            throw new FeiYuException(20001,"执行了自定义异常");
        }
        List<SysRole> list = sysRoleService.list();
        return Result.success(list);
    }
  1. 添加执行方法
package com.feiyu.common.exception;

import com.feiyu.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: KeYu
 * @Package: com.feiyu.common.exception
 * @Date: 2023/05/17/9:05
 * @说明:统一异常处理
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    //自定义异常
    @ExceptionHandler(FeiYuException.class)    //这里是要捕获的异常类FeiYuException(自定义异常类)
    @ResponseBody
    public Result error(FeiYuException e){
        e.printStackTrace();
        return Result.fail().code(e.getCode()).massage(e.getMessage());
    }
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇生产系统CPU飙高问题排查 下一篇Java I/O(1):模型与流

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目