浏览 1089 次
|
锁定老贴子 主题:关于JAVA的异常设计模式
该帖已经被评为新手帖
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2007-10-08 关键字: 异常
最近在做项目struts+spring+hibernate的,感觉自己对于设计异常方面有些不是很明白。已经知道这样的知识:在程序的DAO层中不要捕获异常,而将它抛给Service层,然后Spring的事务声明就写给Service层,最终在Action中来try catch 业务层Service抛出的异常,从而决定页面进行转向,并给用户相应的提示,同时在记录日志。
现在的一个问题就是我每次所抛出的异常应该是什么类型的呢?一般都是说将异常包装自己的类型。可是具体的确怎么设计这一异常体系呢?我也知道是要extends Exception.但是具体的有那些技巧和方法却是一点也不明白。 希望大家指点一下!十分感谢! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2007-10-08
你只知道extends Exception?那你还知不知道 extends RuntimeException?
设计异常一般分: ApplicationException或BusinessException(extends Exception) SystemException或NestUncheckedException(Extends RuntimeException) 你应该在什么时候抛出什么异常与你处理的业务相关,和你的设计相关! |
|
| 返回顶楼 | |
|
最后更新时间:2007-10-08
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之外的异常,如数据格式化异常
这张图片是这个异常体系的结构
我有些想不明白,那些异常错误代码到时候是怎么应用的 |
|
| 返回顶楼 | |
|
最后更新时间:2007-10-08
http://img.blog.163.com/photo/qTwcR-N-cVDdNG38Sq2i2Q==/927178573285209847.jpg这是图片地址
|
|
| 返回顶楼 | |
|
最后更新时间:2007-10-08
还是看不到图片啊,163太恶了
http://boy.365.blog.163.com这是我的那个博客,在相册的第一张就是我在这上面发的图片. |
|
| 返回顶楼 | |
|
最后更新时间:2007-12-03
又一个走火入魔的。。。。。。。。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-02-28
建议去读ThinkingInJava作者关于 Java Exception和 Java Runtime Exception的一篇文章。试试看能不能从理论方面提高一下。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-02-28
抛出异常就是说这东西我没作.....但不是我没考虑到
捕获异常就是说,这类异常用同一个方法来处理.(异常应该是个树型的继承链) |
|
| 返回顶楼 | |
|
最后更新时间:2008-02-28
抛出异常的爱 写道 抛出异常就是说这东西我没作.....但不是我没考虑到
捕获异常就是说,这类异常用同一个方法来处理.(异常应该是个树型的继承链) 看到我都头晕了, 来听首歌吧,让你取暖。 |
|
| 返回顶楼 | |








