自定义业务异常

步骤:
1、创建一个业务异常类,继承RuntimeException

/**
 * 自定义业务异常
 */
public class CustomException extends RuntimeException{

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

 

利用父类构造器构造这个类

 

2、通过全局的异常处理器,将这种类型的异常捕获

    /**
     * 异常处理方法
     * 对CustomException 这个类型的异常进行拦截
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandler(CustomException ex){
        return R.error(ex.getMessage());
    }

  

全局异常处理器的配置方法:

https://www.cnblogs.com/xiaochaoheni/p/16648815.html

posted @ 2022-09-05 10:45  小超和你  阅读(55)  评论(0)    收藏  举报