第一步:自定义异常类:
package com.zhongbin.oa.exception;
/*
* 自定义异常类,用来处理系统的异常
*/
public class SystemException extends RuntimeException {
//对应messageResurser的KEY ,自定义的ExceptionHandler用此
KEY获取异常消息
private String key;
//异常消息的的参数
private Object[] values;
public String getKey() {
return key;
}
public Object[] getValues() {
return values;
}
public SystemException() {
super();
}
public SystemException(String message, Throwable
throwable) {
super(message, throwable);
}
public SystemException(String message) {
super(message);
}
public SystemException(Throwable throwable) {
super(throwable);
}
/*
*@param message 异常消息
*@param key messageResurser的KEY
*/
public SystemException(String message,String key){
super(message);
this.key = key;
}
/*
*@param message 异常消息
*@param key messageResurser的KEY
*@param values key对应异常消息的的单个参数
*/
public SystemException(String message,String key,Object
value){
super(message);
this.key = key;
this.values = new Object[]{value};
}
/*
*@param message 异常消息
*@param key messageResurser的KEY
*@param values key对应异常消息的的参数数组
*/
public SystemException(String message,String key,Object[]
values){
super(message);
this.key = key;
this.values = values;
}
}
第二步:自定义异常处理器
package com.zhongbin.oa.exception;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
public class SystemExceptionHandler extends ExceptionHandler {
/*
* @param ex 异常发生时传递过来的异常实例
* @param ae struts-config.xml 文件中的异常信息的配置
* @param mapping struts-config.xml文件中的mapping信
息配置
* @param formInstance formBean
* @param request request
* @param response response
*/
@Override
public ActionForward execute(Exception ex, ExceptionConfig
ae,
ActionMapping mapping, ActionForm.
formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
//构建一个返回路径
ActionForward forward = null;
if(ae.getPath() != null){
forward = new ActionForward(ae.getPath
());
}else {
forward = mapping.getInputForward();
}
//只处理自定义的SystemException异常类型
if (ex instanceof SystemException) {
SystemException se = (SystemException) ex;
String key = se.getKey();
ActionMessage error = null;
//判断有没有KEY
if(key == null){
key = ae.getKey();
error = new ActionMessage
(key,ex.getMessage());
}else{
//判断有没有values
if(se.getValues() != null){
error = new ActionMessage
(key,se.getValues());
}else{
error = new ActionMessage
(key);
}
}
//调用父类方法设置ERROR 在 SCOPE 中
super.storeException(request, key, error,
forward, ae.getScope());
return forward;
}
//其余的仍交给父类处理
return super.execute(ex, ae, mapping,
formInstance, request, response);
}
}
第三步:设置status-config.xml文件
<global-exceptions>
<exception
key="error.detail"
type="com.zhongbin.oa.exception.SystemException"
scope="request"
handler="com.zhongbin.oa.exception.SystemExceptionHandler"
path="/common/exception.jsp">
</exception>
第四步,设置MessageResources.properties文件key的值
exception.del.org.001 = don't delete orgnization ,id is {0}
error.detail=error:{0}
第五步:在可能有误的地方抛出自定义的异常:
if(org.getChildren().size() > 0 ){
throw new SystemException("不能删除,存在子机构","exception.del.org.001",""+org.getId());
}
分享到:
相关推荐
主要介绍了详解SpringCloud Finchley Gateway 统一异常处理,非常具有实用价值,需要的朋友可以参考下
Spring Boot统一异常处理类,BaseResponse类就两个字段code和message。经测试,可以捕获所以异常,并返回指定json数据
统一异常处理会区分前端是否ajax请求,自动返回json数据格式,要求开发人员在处理ajax请求时统一封装成一个对象返回,以符合代码统一规范。 此工程在idea环境编写,导入请自己新建工程手工复制代码导入。
J2EE项目中统一异常处理,14类异常的自定义分析,支持dao层异常原因的返回
Maven+SpringMVC实现统一异常处理
springboot+redis+shiro单点登录,统一异常处理,统一日志,缓存
springboot统一异常处理
可插拔实现统一异常处理
Springboot全局异常处理demo 项目的开发中,不管是对底层的数据库操作过程,还是业务层的处理过程,还是控制层的处理过程,都不可避免...使用统一异常处理的好处是能够将异常信息统一捕捉并组装成固定格式的数据返回。
3.处理日志时,需要在每一个try-catch块包含一些处理代码,有时候异常处理的代码比正常执行代码还多,污染正常执行代码。 4.同样类型异常有不同的处理方式,使最终的处理变得复杂。 5.接口抛出异常,破坏封装,打破...
主要介绍了Spring Cloud zuul自定义统一异常处理实现,需要的朋友可以参考下
Spring Boot 提供了统一的异常处理机制,通过使用 @ControllerAdvice 和 @ExceptionHandler 注解,可以实现对控制器层、Service 层、Dao 层以及 Spring 系统内定义的部分异常的统一处理。 在 Spring Boot 中,可以...
异常处理是每个应用程序都会用到的,纵然在程序编写期间我们都会最大限度的考虑可能发生的异常并进行相应的处理,但是往往并不能完全考虑周全,百密一疏,出现未处理异常而导致程序出错,数据丢失,如用户输入错误...
本视频针对博客内容,讲解springMVC框架中的统一异常处理的知识点和实例,以及500错误和404错误异常处理上的区别,简单易懂,言简意赅。
统一异常处理、断言和枚举结合使用案例
这篇文章主要介绍了SpringMVC-统一异常处理三种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在 Spring MVC 应用的开发中,不管是对底层数据库操作...
主要给大家介绍了关于Spring Boot统一异常处理最佳实践(拓展篇)的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
该工具类是对Retrofit2+Rxjava2网络请求异常统一封装处理,将onError(Throwable e)中得异常转化成具体的错误信息。除了提示用户以外,还方便开发者准确定位Bug的所在。
主要介绍了Spring Boot 数据校验@Valid+统一异常处理的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
springboot多模块项目,集成了mybatis,连接池,redis,日志,sql日志打印,异常统一处理,统一返回格式,mapper文件自动生成,generator xml ,切面日志和拦截器,sql注入过滤,解压即可部署打包启动,包含数据库...