异常模块(Exception)
Mybatis源码解析一——搭建阅读环境
Mybatis源码解析二——解析器模块(parsing)
Mybatis源码解析三——反射模块(Reflector)
Mybatis源码解析四——异常模块(Exception)
包结构一览
Exceptions的类非常简单
Exception类继承结构
IbatisException
是MyBatis中异常的顶级父类。
IbatisException
下面是这个类的源码,注意注解 @Deprecated,说明这个类已经过期,不推荐使用
/**
* Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.exceptions;
/**
* @author Clinton Begin
*/
@Deprecated
public class IbatisException extends RuntimeException {
private static final long serialVersionUID = 3880206998166270511L;
public IbatisException() {
super();
}
public IbatisException(String message) {
super(message);
}
public IbatisException(String message, Throwable cause) {
super(message, cause);
}
public IbatisException(Throwable cause) {
super(cause);
}
}
PersistenceException
PersistenceException
代替了上述的IbatisException
类,在关系上,继承了IbatisException
类,源码如下
/**
* Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.exceptions;
/**
* @author Clinton Begin
*/
@SuppressWarnings("deprecation")
public class PersistenceException extends IbatisException {
private static final long serialVersionUID = -7537395265357977271L;
public PersistenceException() {
super();
}
public PersistenceException(String message) {
super(message);
}
public PersistenceException(String message, Throwable cause) {
super(message, cause);
}
public PersistenceException(Throwable cause) {
super(cause);
}
}
类似的还有一些异常类,都比较简单,只是在业务含义上做区分。
ExceptionFactory
/**
* Copyright 2009-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.exceptions;
import org.apache.ibatis.executor.ErrorContext;
/**
* @author Clinton Begin
*/
public class ExceptionFactory {
private ExceptionFactory() {
// Prevent Instantiation
}
public static RuntimeException wrapException(String message, Exception e) {
return new PersistenceException(ErrorContext.instance().message(message).cause(e).toString(), e);
}
}
异常工程,就是将捕获的异常和异常描述信息进行封装,统一的返回RuntimeException
,说集体点就是PersistenceException
。
使用案例如下:
public SqlSessionFactory build(Reader reader, String environment, Properties properties) {
try {
XMLConfigBuilder parser = new XMLConfigBuilder(reader, environment, properties);
return build(parser.parse());
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error building SqlSession.", e);
} finally {
ErrorContext.instance().reset();
try {
reader.close();
} catch (IOException e) {
// Intentionally ignore. Prefer previous error.
}
}
}