统一异常处理
  8fwIp6f95Tvz 2023年11月01日 39 0

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());
    }
}
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年05月12日 0

暂无评论

推荐阅读
  2Vtxr3XfwhHq   2024年05月17日   55   0   0 Java
  Tnh5bgG19sRf   2024年05月20日   110   0   0 Java
  8s1LUHPryisj   2024年05月17日   46   0   0 Java
  aRSRdgycpgWt   2024年05月17日   47   0   0 Java
8fwIp6f95Tvz
作者其他文章 更多