异常处理的问题

airfox2000 2004-10-09 03:40:32
写了这样一个函数
public Object executeSingleQuery(String strPoolName,String sqlString)
{
Statement stmt = null;
ResultSet rs = null;
Object returnObject = null;
Connection conn = null;
try{
conn = getConnection(strPoolName);
stmt = conn.createStatement();
rs = stmt.executeQuery(sqlString);
if(rs.next()){
returnObject = rs.getObject(1);
}
rs.close();
stmt.close();
conn.commit();
freeConnection(strPoolName,conn);
}catch (SQLException e){
log(e.toString()+",执行查询语句失败");
rs.close();
stmt.close();
freeConnection(strPoolName,conn);
throw e;
}
return returnObject;
}

提示throw e这行有错,错误提示为
Error #: 360 : unreported exception: java.sql.SQLException; must be caught or declared to be thrown at line 235, column 5
我觉得没有错啊,为什么?当把catch里的
rs.close();
stmt.close();
throw e;
去掉则正常了,但这不符合要求啊,请高手帮忙哦!

...全文
110 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
XqYuan 2004-10-10
  • 打赏
  • 举报
回复
如果你抛出的错误不是RuntimeException,就应该在方法中throws,如果是RuntimeException就不用
airfox2000 2004-10-09
  • 打赏
  • 举报
回复
我知道如何解决了,函数声明要改
public Object executeSingleQuery(String strPoolName,String sqlString) throws SQLException

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧