错误描述
Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor
默认构造函数无法处理隐式超级构造函数引发的异常类型IOException。必须定义显式构造函数。
错误代码
//测试类中
private IDpetService service = new DepartmentServiceImpl();
//实现类中 错误位置
public DepartmentServiceImpl() throws IOException {
session = MybatisSupport.connMybatis();
mapper = session.getMapper(DepartmentMapper.class);
}
错误原因
在实现类的构造器中,使用了 throws Exception 来抛出异常.(非常不应该的错误)
解决方法
使用try …catch 解决异常抛出的问题
//这样写就没问题了
public DepartmentServiceImpl(){
try {
session = MybatisSupport.connMybatis();
mapper = session.getMapper(DepartmentMapper.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}