自动式异常处理

2022-08-19 13:08:26
关于异常的处理,大概就有编程式的异常处理,自动式异常处理,声明式的异常处理,编程式的异常处理一般是不可能用,因为在项目开发的过程中,绝大部分是采用声明式的异常处理,每个项目都有自己的异常处理类。这里发个自动式异常处理,对理解声明式的异常处理有点帮助。
1.在业务逻辑层抛出异常:简单抛出个RuntimeException

public void delOrg(int orgId) {
Orgnization org = (Orgnization)this.getHibernateTemplate().get(Orgnization.class, new Integer(orgId));
if(org.getChildren().size() > 0){
throw new RuntimeException("存在子机构,无发删除");
}
this.getHibernateTemplate().delete(org);

}

2.抛出异常后,只要在struts-config.xml的对应的action中声明怎样处理就行
<exception key="errors.detail" type="java.lang.RuntimeException" scope="request" path="/common/exception.jsp"/>

当然,你还用配置国际化资源文件和path所对应的文件,如下
exception.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/common/common.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
</body>
</html>


在国际化资源文件里的配置如下:

errors.detail={0}
  • 作者:iteye_17392
  • 原文链接:https://blog.csdn.net/iteye_17392/article/details/81681568
    更新时间:2022-08-19 13:08:26