自定义异常类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GuliException extends RuntimeException {

    /**异常状态码*/
    private Integer code;
    /**异常信息*/
    private String msg;
}
# 统一异常类
```java
@ControllerAdvice
public class exceptionHandler {


    @ExceptionHandler(Exception.class)
    /**返回数据
     * 返回的为json数据
     * */
    @ResponseBody
    public R error(Exception e){
        e.printStackTrace();
        return R.error().message("执行了全局异常处理");
    }

    /**
     * 执行了特定异常处理
     * */
    @ExceptionHandler(ArithmeticException.class)
    /**返回数据
     * 返回的为json数据
     * */
    @ResponseBody
    public R error(ArithmeticException e){
        e.printStackTrace();
        return R.error().message("执行了ArithmeticException异常处理");
    }

    /**
     * 自定义异常类
     * */
    @ExceptionHandler(GuliException.class)
    /**返回数据
     * 返回的为json数据
     * */
    @ResponseBody
    public R error(GuliException e){
        e.printStackTrace();
        return R.error().code(e.getCode()).message(e.getMsg());
    }


}

posted @ 2020-08-21 00:16  池三岁  阅读(220)  评论(0编辑  收藏  举报