自定义业务异常
步骤:
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());
}
浙公网安备 33010602011771号