需求:需要自定义异常中带有错误码,便于接口返回错误信息。
UserException 自定义异常类:
public class UserException extends RuntimeException{
private static final long serialVersionUID = 1L;
private Integer code;
public UserException(){
super();
}
public UserException(String msg,Integer errorCode){
super(msg);
code=errorCode;
}
public Integer getCode() {
return code;
}
}
抛出异常:
throw new UserException("用户名已存在",3001);
catch异常:
public PageInfo test(HttpServletRequest request) {
PageInfo pageInfo=new PageInfo();
try {
userService.test();
} catch (UserException e) {
pageInfo.setCode(e.getCode());
pageInfo.setMsg(e.getMessage());
}
return pageInfo;
}
话说这周要记得东西有点多啊。。。我都写不过来了!喵的!