自定义异常
1.java中异常均继承自Throwable,其有两个重要的直接子类error与exception;
2.java错误error,大部分是由虚拟机爆出来的错误,是程序无法处理的错误,如 OutOfMemoryError,当JVM需要更多内存空间而得不到满足时,就会爆出OutOfMemoryError;
3.Exception,异常,其下分类很多,如可查异常与不可查异常,运行时异常与非运行时异常,基本概念一样,只是说法不同罢了。其有个重要的子类即RuntimeException运行时异常,其它直接子类都归为非RuntimeException,如IOException,SQLException等;
a.非RuntimeException(非运行时异常)是在代码书写时,编译器给你检查提示你要进行try catch或throws处理。
b.RuntimeException(运行时异常),编译器不会帮你自动检查,当你运行程序时,虚拟机才会给你爆出错误让你去处理,这个往往是我们编码逻辑或不规范导致的。
异常与错误的类和接口已如此之多,为什么还要自定义异常?我初入行时原来也有过困惑,但是随着项目经验及代码的规范化,自定义异常已经是项目必不可少的一步了。如下是在项目中经常定义的业务异常(BusinessException),不符合业务数据的情况所抛出的异常:
@Getter
public class BusinessException extends Exception {
private int code;
public BusinessException(int code,String message) {
super(message);
this.code = code;
}
public BusinessException(ResponseEnum responseEnum){
super(responseEnum.getMessage());
this.code=responseEnum.getCode();
}
//直接构建系统内部错误
public static BusinessException systemException(String message){
return new BusinessException(ResponseEnum.SYSTEM_ERROR.getCode(),message);
}
}
可以在service中使用,业务性异常直接抛出:
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public int addUser(User user) throws Exception{
//如果名字为空,直接抛出异常
if(StringUtils.isEmpty(user.getUserName())){
throw new BusinessException(40001,"必要参数缺失");
}
return userMapper.addUser(user);
}
}
自定义异常的好处:
1.异常说明明确,能够快速定位问题;
2.统一代码业务性问题处理规范;
3.方便错误数据的封装,后台数据机构的统一(统一异常拦截体现);
4.其他…项目暂时理解就这些
异常统一拦截
返回的异常,都需要经过统一的处理,同样spring也提供了接口对异常返回的建议:
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public BaseResponse jsonErrorHandler(HttpServletRequest request, Exception e) throws Exception{
BaseResponse response = new BaseResponse();
if( e instanceof BusinessException ){
response.setCode(((BusinessException)e).getCode());
response.setMessage(e.getMessage());
}else{
log.error(e.getMessage(),e);
response.setCode(ResponseEnum.SYSTEM_ERROR.getCode());
response.setMessage(ResponseEnum.SYSTEM_ERROR.getMessage());
}
return response;
}
}
@ControllerAdvice:
是一个@Component,用于定义@ExceptionHandler,@InitBinder和@ModelAttribute方法,适用于所有使用@RequestMapping方法。有兴趣的可以去了解一下。
总结:
数据统一拦截封装返回,自定义异常,异常统一拦截处理。让平时在写程序中,更多的去关注业务问题,数据的最初状态即可,后期扩展也方便。还是比较实用的。