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

关于JAVA的错误设计模式

2012-11-01 
关于JAVA的异常设计模式最近在做项目struts+spring+hibernate的,感觉自己对于设计异常方面有些不是很明白。

关于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之外的异常,如数据格式化异常

这张图片是这个异常体系的结构


我有些想不明白,那些异常错误代码到时候是怎么应用的 3 楼 qichunren 2007-10-08   /uploadfile/jiaocheng/20140186/2839/2014012822392819712.jpg这是图片地址 4 楼 qichunren 2007-10-08   还是看不到图片啊,163太恶了
http://boy.365.blog.163.com这是我的那个博客,在相册的第一张就是我在这上面发的图片. 5 楼 超级莱鸟 2007-12-03   又一个走火入魔的。。。。。。。。 6 楼 greens.leaf 2008-02-28   建议去读ThinkingInJava作者关于 Java Exception和 Java Runtime Exception的一篇文章。试试看能不能从理论方面提高一下。 7 楼 抛出异常的爱 2008-02-28   抛出异常就是说这东西我没作.....但不是我没考虑到
捕获异常就是说,这类异常用同一个方法来处理.(异常应该是个树型的继承链) 8 楼 richardlovejob 2008-02-28   抛出异常的爱 写道抛出异常就是说这东西我没作.....但不是我没考虑到
捕获异常就是说,这类异常用同一个方法来处理.(异常应该是个树型的继承链)
看到我都头晕了, 来听首歌吧,让你取暖。

热点排行