集成spring框架的统一异常处理

JDBC的异常转换器 
    传统的JDBC API在发生几乎所有的数据操作问题都抛出相同的SQLException,它将异常的细节性信息封装在异常属性中,所以如果希望了解异常的具体原因,你必须分析异常对象的信息。 

    SQLException拥有两个代表异常具体原因的属性:错误码和SQL状态码,前者是数据库相关的,可通过getErrorCode()返回,其值的类型是int;而后者是一个标准的错误代码,可通过getSQLState()返回,是一个String类型的值,由5字符组成。 

     Spring根据错误码和SQL状态码信息将SQLExeption翻译成Spring DAO的异常体系。在org.springframework.jdbc.support包中定义了SQLExceptionTranslator接口,该接口的两个实现类SQLErrorCodeSQLExceptionTranslator和SQLStateSQLExceptionTranslator分别负责处理SQLException中错误代码和SQL状态码的翻译工作。将SQLException翻译成Spring DAO异常体系的工作是比较艰辛的,但Spring框架替我们完成这项艰巨的工作并保证转换的正确性,我们有充分的理由依赖这个转换的正确性。

      spring这样做主要有2个作用,一是统一建立spring框架的异常框架体系,使得不同的异常最后都封装转换成spring异常体系框架中,这个框架体系分类很详细,这样对问题也比较好定位。

      二是使得spring对底层异常保持独立,涡轮采用什么样的dao层,都不影响spring的异常体系。

 

    异常的基本概念:

1.RuntimeException,也就是运行时异常,表示你的代码本身存在BUG,比如你提到的ArrayIndexOutOfBoundsException,数组下标越界,这个属于代码有问题,数组定义的长度不够实际使用,不处理肯定会报错,如果你操作某个模块发现能正常运行,那只是因为代码还没跑到这个错误的地方而已。。控制台一旦报RuntimeException,就必须要处理。。没有例外的。而且,处理RuntimeException,不是try-catch能解决的。。try-catch在这里使用毫无意义。
2.不是RuntimeException,就是编译时异常,异常只有这两种了。比如你在处理文件流时的I/O问题,就属于编译时异常。这个时候用thr{}catch 来捕获或者 throws即可。
3.error,就不在这里赘述了

实现代码:

import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.PermissionDeniedDataAccessException;
import org.springframework.dao.RecoverableDataAccessException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.UncategorizedSQLException;

/**
 * 统一捕获异常
 * @author zhengjunxiang
 *
 */
public class CaptureException {
	
	//处理异常方法
	private void catchException(){
		try{
			
		}catch(BadSqlGrammarException e1){//NonTransient
			/**
			 * Exception thrown when SQL specified is invalid. Such exceptions always have
			 * a <code>java.sql.SQLException</code> root cause.
			 *
			 * <p>It would be possible to have subclasses for no such table, no such column etc.
			 * A custom SQLExceptionTranslator could create such more specific exceptions,
			 * without affecting code using this class.
			 *
			 * @author Rod Johnson
			 * @see InvalidResultSetAccessException
			 */
		}catch(DataIntegrityViolationException e2){
			/**
			 * Exception thrown when an attempt to insert or update data
			 * results in violation of an integrity constraint. Note that this
			 * is not purely a relational concept; unique primary keys are
			 * required by most database types.
			 *
			 * @author Rod Johnson
			 */
		}catch(DataAccessResourceFailureException e3){
			/**
			 * Data access exception thrown when a resource fails completely:
			 * for example, if we can't connect to a database using JDBC.
			 *
			 * @author Rod Johnson
			 * @author Thomas Risberg
			 */
		}catch(PermissionDeniedDataAccessException e5){
			/**
			 * Exception thrown when the underlying resource denied a permission
			 * to access a specific element, such as a specific database table.
			 *
			 * @author Juergen Hoeller
			 * @since 2.0
			 */
		}catch(ConcurrencyFailureException e6){//Transient
			/**
			 * Exception thrown on concurrency failure.
			 *
			 * <p>This exception should be subclassed to indicate the type of failure:
			 * optimistic locking, failure to acquire lock, etc.
			 *
			 * @author Thomas Risberg
			 * @since 1.1
			 * @see OptimisticLockingFailureException
			 * @see PessimisticLockingFailureException
			 * @see CannotAcquireLockException
			 * @see DeadlockLoserDataAccessException
			 */
		}catch(TransientDataAccessResourceException e7){
			/**
			 * Data access exception thrown when a resource fails temporarily
			 * and the operation can be retried.
			 *
			 * @author Thomas Risberg
			 * @since 2.5
			 * @see java.sql.SQLTransientConnectionException
			 */
		}catch(RecoverableDataAccessException e8){//Recoverable
			/**
			 * Data access exception thrown when a previously failed operation might be able
			 * to succeed if the application performs some recovery steps and retries the entire
			 * transaction or in the case of a distributed transaction, the transaction branch.
			 * At a minimum, the recovery operation must include closing the current connection
			 * and getting a new connection.
			 *
			 * @author Thomas Risberg
			 * @since 2.5
			 * @see java.sql.SQLRecoverableException
			 */
		}catch(UncategorizedSQLException e9){
			/**
			 * Exception thrown when we can't classify a SQLException into 
			 * one of our generic data access exceptions.
			 *
			 * @author Rod Johnson
			 * @author Juergen Hoeller
			 */
		}catch(DataAccessException e10){	//所以异常的基类,下包括事务异常和非事务异常,以上是spring抛出的SqlException异常
			/**
			 * Root of the hierarchy of data access exceptions discussed in
			 * <a href="http://www.amazon.com/exec/obidos/tg/detail/-/0764543857/">Expert One-On-One J2EE Design and Development</a>.
			 * Please see Chapter 9 of this book for detailed discussion of the
			 * motivation for this package.
			 *
			 * <p>This exception hierarchy aims to let user code find and handle the
			 * kind of error encountered without knowing the details of the particular
			 * data access API in use (e.g. JDBC). Thus it is possible to react to an
			 * optimistic locking failure without knowing that JDBC is being used.
			 *
			 * <p>As this class is a runtime exception, there is no need for user code
			 * to catch it or subclasses if any error is to be considered fatal
			 * (the usual case).
			 *
			 * @author Rod Johnson
			 */
		}catch(NumberFormatException nfe){
			/**
			 * 数字转换异常
			 */
		}catch(Throwable e){
			/**
			 * 异常的最基类
			 */
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值