美文网首页
javaweb异常管理

javaweb异常管理

作者: pr0metheus | 来源:发表于2018-03-21 21:25 被阅读0次

javaweb中一般将异常进行集中管理,这样的好处就是维护方便

首先我们需要一个类ErrorCode来管理错误代码常量值

代码如下:

package com.shengsiyuan.imis.exception;

public class ErrorCode {

    //数据源层面的异常
    /////////////////////////////////////////////////////////////////////////
    //数据库连接错误
    public final static int DB_CONNECTION_ERROR = 0000;
    //数据库提交错误
    public final static int DB_COMMIT_ERROR = 0001;
    //数据库回滚错误
    public final static int DB_ROLLBACK_ERROR = 0002;
    
    //Dao层面的异常
    /////////////////////////////////////////////////////////////////////////
    
    public final static int SQL_ERROR = 1000;
    
    //Service层面的异常
    /////////////////////////////////////////////////////////////////////////
    //显示新闻类别错误
    public final static int LIST_NEWSCATALOG_ERROR= 2000;
    //增加新闻类别错误
    public final static int ADD_NEWSCATALOG_ERROR= 2001;
    //更新新闻类别错误
    public final static int UPDATE_NEWSCATALOG_ERROR= 2002;
    
    //其它异常
    /////////////////////////////////////////////////////////////////////////
    //新闻类别已经存在
    public final static int REPEAT_NEWSCATALOG_ERROR= 9900;
    
    
}

接着我们需要一个辅助类MessageHelper,该类根据错误代码值来获得中文释义。该类采用ResourceBundle来读取配置文件,并通过ResourceBundle的getString(key)来获得value值。具体代码如下:

package com.shengsiyuan.imis.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class MessageHelper {

    public static String getExceptionInfo(int errorCode) {
        ResourceBundle rb = ResourceBundle.getBundle(
                FileNameString.EXCEPTION_INFO_NAME, Locale.getDefault());
        String exceptionMessage = rb.getString(String.valueOf(errorCode));
        return exceptionMessage;
    }
}

因为javaweb是通过servlet调用service,service调用dao的方式进行的,为了方便定位异常的来源因此对service层以及dao层都自定义一个异常类即ServiceException和DaoException这样当在Dao层出现异常则抛出DaoException,而在service层进行捕获并抛出ServiceException,而在Servlet层则不抛出只打印到控制台(注:个人觉得在该层出现异常则写日志),为什么呢?因为在servlet层抛出异常则直接到浏览器了,异常处理有一个原则就是不能把未处理过的异常信息直接反馈到浏览器,因此不在该层抛出异常。

为了编程方便(抛异常方便)将错误代码值作为异常对象的构造参数,代码如下:

package com.shengsiyuan.imis.exception;

import com.shengsiyuan.imis.util.MessageHelper;

/**
 * 
 * <p>Title: DaoException</p>
 * <p>Description: Dao层抛出的异常对象</p>
 * <p>Company: 自由职业</p> 
 * @author lsw
 * @date 2018年2月10日
 */
public class DaoException extends Exception {
    
    private int errorCode;

    public DaoException(Throwable e) {
        super(e);
    }
    
    public DaoException(int errorCode) {
        super(MessageHelper.getExceptionInfo(errorCode));
        this.errorCode = errorCode;
    }
    
    public DaoException(int errorCode, Throwable cause) {
        super(MessageHelper.getExceptionInfo(errorCode), cause);
        this.errorCode = errorCode;
    }
    
    public DaoException(String message) {
        super(message);
    }
    
    public DaoException(String message, Throwable cause) {
        super(message, cause);
    }
    
    public String getExceptionMessage() {
        return getMessage();
    }
    
    public int getErrorCode() {
        return errorCode;
    }
}

ServiceException同理

相关文章

网友评论

      本文标题:javaweb异常管理

      本文链接:https://www.haomeiwen.com/subject/qiunqftx.html