统一异常处理

  金牌会员 | 2023-5-17 10:06:34 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 889|帖子 889|积分 2667

Spring Boot中的统一异常处理


  • Result为封装传递给前端的包装类
  • 全局异常处理
  1. /**
  2. * Created with IntelliJ IDEA.
  3. *
  4. * @Author: KeYu
  5. * @Package: com.feiyu.common.exception
  6. * @Date: 2023/05/17/9:05
  7. * @说明:统一异常处理
  8. */
  9. @ControllerAdvice
  10. public class GlobalExceptionHandler {
  11.     /**
  12.      * 全局异常处理,执行方法
  13.      * @param e
  14.      * @return
  15.      */
  16.     @ExceptionHandler(Exception.class)   //这里是要捕获的异常类Exception
  17.     @ResponseBody                        //这里返回数据
  18.     public Result error(Exception e){
  19.         e.printStackTrace();             //输出错误
  20.         return Result.fail().massage("执行了全局异常处理。。。。。");
  21.     }
  22. }
复制代码

  • 特定异常处理
  1. /**
  2. * Created with IntelliJ IDEA.
  3. *
  4. * @Author: KeYu
  5. * @Package: com.feiyu.common.exception
  6. * @Date: 2023/05/17/9:05
  7. * @说明:统一异常处理
  8. */
  9. @ControllerAdvice
  10. public class GlobalExceptionHandler {
  11.     /**
  12.      * 特定异常处理
  13.      * @param e
  14.      * @return
  15.      */
  16.     @ExceptionHandler(ArithmeticException.class)    //这里是要捕获的异常类ArithmeticException
  17.     @ResponseBody
  18.     public Result error(ArithmeticException e){
  19.         e.printStackTrace();
  20.         return Result.fail().massage("执行了特定异常处理。。。。。");
  21.     }
  22. }
复制代码

  • 自定义异常处理

  • 创建异常类,继承RuntimeException
  1. /**
  2. * Created with IntelliJ IDEA.
  3. *
  4. * @Author: KeYu
  5. * @Package: com.feiyu.common.exception
  6. * @Date: 2023/05/17/9:23
  7. * @说明:自定义异常类
  8. */
  9. @Data
  10. public class FeiYuException extends RuntimeException{
  11.     private Integer code;
  12.     private String msg;
  13.     public FeiYuException(Integer code,String msg){
  14.         super(msg);
  15.         this.code = code;
  16.         this.msg = msg;
  17.     }
  18.     /**
  19.      * 接收枚举类对象
  20.      */
  21.     public FeiYuException(ResultCodeEnum resultCodeEnum){
  22.         super(resultCodeEnum.getMessage());
  23.         this.code = resultCodeEnum.getCode();
  24.         this.msg=resultCodeEnum.getMessage();
  25.     }
  26. }
复制代码

  • 在异常的地方,手动添加异常
  1. @ApiOperation("查询所有角色")
  2.     @GetMapping("/findAll")
  3.     public Result findAll(){
  4.         try {
  5.             int a = 10/0;
  6.         }catch (Exception e){
  7.             //抛出自定义异常
  8.             throw new FeiYuException(20001,"执行了自定义异常");
  9.         }
  10.         List<SysRole> list = sysRoleService.list();
  11.         return Result.success(list);
  12.     }
复制代码

  • 添加执行方法
  1. package com.feiyu.common.exception;
  2. import com.feiyu.result.Result;
  3. import org.springframework.web.bind.annotation.ControllerAdvice;
  4. import org.springframework.web.bind.annotation.ExceptionHandler;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. /**
  7. * Created with IntelliJ IDEA.
  8. *
  9. * @Author: KeYu
  10. * @Package: com.feiyu.common.exception
  11. * @Date: 2023/05/17/9:05
  12. * @说明:统一异常处理
  13. */
  14. @ControllerAdvice
  15. public class GlobalExceptionHandler {
  16.     //自定义异常
  17.     @ExceptionHandler(FeiYuException.class)    //这里是要捕获的异常类FeiYuException(自定义异常类)
  18.     @ResponseBody
  19.     public Result error(FeiYuException e){
  20.         e.printStackTrace();
  21.         return Result.fail().code(e.getCode()).massage(e.getMessage());
  22.     }
  23. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表