首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

自定义错误

2012-08-31 
自定义异常自定义的一个异常 准备在业务里抛出package com.pindou.exception.userservicepublic class Up

自定义异常
自定义的一个异常 准备在业务里抛出

package com.pindou.exception.userservice;public class UpdateUserInfoException extends Exception {/** *  */private static final long serialVersionUID = 1237862166695963054L;/** *  */public UpdateUserInfoException() {// TODO Auto-generated constructor stub}/** * @param message */public UpdateUserInfoException(String message) {super(message);// TODO Auto-generated constructor stub}/** * @param cause */public UpdateUserInfoException(Throwable cause) {super(cause);// TODO Auto-generated constructor stub}/** * @param message * @param cause */public UpdateUserInfoException(String message, Throwable cause) {super(message, cause);// TODO Auto-generated constructor stub}}


在业务里抛出时
public BaseResponse<LoginReturnDto> updateUserInfo(UpdateUserInfoRequest request) throws DataAccessException,UpdateUserInfoException {try {UserInfo userInfoNew = userInfoDao.getUserInfoById(request.getUserId());LoginReturnDto returnDto = getUpdateUserInfoReturnDto(userInfoNew);BaseResponse<LoginReturnDto> response = new BaseResponse<LoginReturnDto>();response.setCode(CodeDefinition.SUCCESS);response.setData(returnDto);response.setMessage("修改成功!");return response;} catch (Exception e) {//logger.error(e.getMessage(),e);throw new UpdateUserInfoException(e);}}

如果在捕获的时候不打出异常,而是在上层调用此业务的时候去打印,那么如果业务里出现了异常只能定位到throw new UpdateUserInfoException(e); 这一行 不能定位到业务里的具体某一行里,所以在catch (Exception e)后要把异常信息打印出来,这样就行定位到 catch里的异常信息了  所以catch后面还是都打出日志吧。

热点排行