关于JAVA的异常设计模式
最近在做项目struts+spring+hibernate的,感觉自己对于设计异常方面有些不是很明白。已经知道这样的知识:在程序的DAO层中不要捕获异常,而将它抛给Service层,然后Spring的事务声明就写给Service层,最终在Action中来try catch 业务层Service抛出的异常,从而决定页面进行转向,并给用户相应的提示,同时在记录日志。
现在的一个问题就是我每次所抛出的异常应该是什么类型的呢?一般都是说将异常包装自己的类型。可是具体的确怎么设计这一异常体系呢?我也知道是要extends Exception.但是具体的有那些技巧和方法却是一点也不明白。
希望大家指点一下!十分感谢! 我还是有些不明白,我给大家贴出一个异常设计的代码出来,大家评评啊!
package org.appfuse.util.exception;public class ExceptionConstants { /** Not an Exception */ public static final int SUCCESS = 0; // System wide error section /** General Application System Error: 1-100 */ public static final int SYS_GENERAL_ERR = 1; // DB Error section: 1001 - 1999. /** Common DB Error */ public static final int DB_COMMON_ERR = 1001; /** Connection Error */ public static final int DB_CONN_ERR = 1002; // Business Exception section: 2000 - 2999. public static final int NAME_REQUIRED = 2001; public static final int PASSWD_REQUIRED = 2003; public static final int NAMEORPASSWDERROR = 2002;}这是个异常常量类,定义那些错误类型代码.
package org.appfuse.util.exception;import java.text.SimpleDateFormat;import java.util.Date;import java.io.PrintStream;import java.io.PrintWriter;public abstract class AppException extends Exception { /** Exception Error Code */ private int errorCode = ExceptionConstants.SUCCESS; /** Exception ID */ private String exceptionId; /** Exception Cause */ protected Throwable cause; /** * Construct Exception with error code. * * @param errorCode int */ public AppException(int errorCode) { super(); this.errorCode = errorCode; genExceptionId(); } /** * Construct Exception with message and error code. * @param message exception indication message * @param errorCode the corresponding error code */ public AppException(String message, int errorCode) { super(message); this.errorCode = errorCode; genExceptionId(); } /** * Construct exception with nested cause and error code. * @param cause the nested exception * @param errorCode the error code */ public AppException(Throwable cause, int errorCode) { super(cause); this.cause = cause; this.errorCode = errorCode; genExceptionId(); } /** * Construct exception with cause, message and error code. * @param message exception indication message * @param cause the nested cause * @param errorCode the error code */ public AppException(String message, Throwable cause, int errorCode) { super(message, cause); this.errorCode = errorCode; this.cause = cause; genExceptionId(); } /** * Get exception error code * @return the exception error code */ public int getErrorCode() { return errorCode; } public void setErrorCode(int errorCode) { this.errorCode = errorCode; } /** * Generate exception id. */ public void genExceptionId() { String idFormat = "yyMMddHHmmssS"; SimpleDateFormat sdf = new SimpleDateFormat(idFormat); this.exceptionId = sdf.format(new Date()); } public String getExceptionId() { return exceptionId; } public void setExceptionId(String exceptionId) { this.exceptionId = exceptionId; } /** * Print Exception Stack trace. * @param s PrintStream */ public void printStackTrace(PrintStream s) { s.println("<app_exception>\r\n\t<exception_id>" + exceptionId + "</exception_id>\r\n"); s.println("\t<exception-localized-message>" + "ErrorCode=" + getErrorCode() + " LocalizedMessage=" + getLocalizedMessage() + "</exception-localized-message>\r\n\t<exception_stack_trace>"); //if (cause != null) { // cause.printStackTrace(s); //} super.printStackTrace(s); s.println("\t</exception_stack_trace>\r\n</app_exception>"); } public void printStackTrace(PrintWriter s) { s.println("<app_exception>\r\n\t<exception_id>" + exceptionId + "</exception_id>\r\n"); s.println("\t<exception-localized-message>" + "ErrorCode=" + getErrorCode() + " LocalizedMessage=" + getLocalizedMessage() + "</exception-localized-message>\r\n\t<exception_stack_trace>"); //if (cause != null) { // cause.printStackTrace(s); //} super.printStackTrace(s); s.println("\t</exception_stack_trace>\r\n</app_exception>"); }}这是抽象类,程序中所有的异常都继承此类
package org.appfuse.util.exception;public class AppDAOException extends AppSystemException { public AppDAOException(int errorCode) { super(errorCode); } public AppDAOException(String message, int errorCode) { super(message, errorCode); } public AppDAOException(Throwable cause, int errorCode) { super(cause, errorCode); } public AppDAOException(String message, Throwable cause, int errorCode) { super(message, cause, errorCode); }}这是持久层的异常,抛给业务层后,业务层捕获后再封装成AppSystemException异常
package org.appfuse.util.exception;public class AppSystemException extends AppException { public AppSystemException(int errorCode) { super(errorCode); } public AppSystemException(String message, int errorCode) { super(message, errorCode); } public AppSystemException(Throwable cause, int errorCode) { super(cause, errorCode); } public AppSystemException(String message, Throwable cause, int errorCode) { super(message, cause, errorCode); }}这是应用程序异常,由业务层抛出
package org.appfuse.util.exception;public class AppBusinessException extends AppException { public AppBusinessException(int errorCode) { super(errorCode); } public AppBusinessException(String message, int errorCode) { super(message, errorCode); } public AppBusinessException(Throwable cause, int errorCode) { super(cause, errorCode); } public AppBusinessException(String message, Throwable cause, int errorCode) { super(message, cause, errorCode); }}这是业务层异常,是指DAO之外的异常,如数据格式化异常