strut1 spring Hibernate 框架:
在action类中抛出的异常我想在Servlet 捕捉 结果捉不到自定义的AppException ,只能捉到Exception 请教这是怎么回事啊
?
public class UserAction extends BaseAction {
public ActionForward adduser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { User user = new User();
organizationService.saveUser(user);//此处抛出自定义异常AppException }
}
public class ControllerActionServlet extends ActionServlet {
if(bRunProcess){
boolean errorFlag = false;
String errorMsg = "";
try{
this.process(request, response);
}catch(AppException eee){
//这段代码永远走不到 errorFlag = true;
errorMsg = eee.getErrorMessage();
if(errorMsg!=null)//add by tanchao for solve the null point
errorMsg = errorMsg.replace(":", ":");
}catch(Exception eee){ 直接到这里了
errorFlag = true;
errorMsg = eee.getMessage();
if(errorMsg!=null)//add by tanchao for solve the null point
errorMsg = errorMsg.replace(":", ":");
}
}
在action类中抛出的异常我想在Servlet 捕捉 结果捉不到自定义的AppException ,只能捉到Exception 请教这是怎么回事啊
?
public class UserAction extends BaseAction {
public ActionForward adduser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception { User user = new User();
organizationService.saveUser(user);//此处抛出自定义异常AppException }
}
public class ControllerActionServlet extends ActionServlet {
if(bRunProcess){
boolean errorFlag = false;
String errorMsg = "";
try{
this.process(request, response);
}catch(AppException eee){
//这段代码永远走不到 errorFlag = true;
errorMsg = eee.getErrorMessage();
if(errorMsg!=null)//add by tanchao for solve the null point
errorMsg = errorMsg.replace(":", ":");
}catch(Exception eee){ 直接到这里了
errorFlag = true;
errorMsg = eee.getMessage();
if(errorMsg!=null)//add by tanchao for solve the null point
errorMsg = errorMsg.replace(":", ":");
}
}
HttpServletRequest request, HttpServletResponse response)
throws Exception {User user = new User();
organizationService.saveUser(user);//此处抛出自定义异常AppException }
红字部分改成throws AppException
这个方法中别try catch直接throws异常。
没抛!怎么会得到!
自定义异常也要
organizationService.saveUser(user);
} catch (Exception e) {
throw new AppException("保存失败",e.getMessage());
}
方法里面肯定能抛出 AppException 但是就是在servlet、中抓不到你直接抛出Exception 的话 servlet中不知道后台跑出来的是什么错误啊 比如说用户名重复
organizationService.saveUser(user);
}catch(自定义异常){
throw new 自定义异常
}
HttpServletRequest request, HttpServletResponse response)
throws APPException {User user = new User();
throws AppException(""); //还是不行啊 悲剧
}
}
然后在action里使用
try{
//逻辑
}catch(Exception e){
if(e instanceof AppException){
//错误处理逻辑
}}