迭代器实现应该如何处理检查异常?
我将 java.sql.RecordSet 包装在 java.util.Iterator 中。我的问题是,如果任何记录集方法抛出 SQLException,我该怎么办?
java.util.Iterator javadoc解释了在各种情况下抛出哪些异常(即,如果您在最后一个元素之外调用 next(),则为 NoSuchElementException)
。但是,它没有提到当出现由网络或磁盘 IO 问题等完全不相关的问题时该怎么做。
简单地在 next() 和 hasNext() 中抛出 SQLException 是不可能的,因为它与 Iterator 接口不兼容。
这是我当前的代码(简化):
public class MyRecordIterator implements Iterator<Record>
{
private final ResultSet rs;
public MyRecordIterator() throws SQLException
{
rs = getConnection().createStatement().executeQuery(
"SELECT * FROM table");
}
@Override
public boolean hasNext()
{
try
{
return !rs.isAfterLast();
}
catch (SQLException e)
{
// ignore, hasNext() can't throw SQLException
}
}
@Override
public Record next()
{
try
{
if (rs.isAfterLast()) throw new NoSuchElementException();
rs.next();
Record result = new Record (rs.getString("column 1"), rs.getString("column 2")));
return result;
}
catch (SQLException e)
{
// ignore, next() can't throw SQLException
}
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Iterator is read-only");
}
}
I'm wrapping a java.sql.RecordSet inside a java.util.Iterator. My question is, what should I do in case any recordset method throws an SQLException?
The java.util.Iterator javadoc explains which exceptions to throw in various situations (i.e. NoSuchElementException in case you call next() beyond the last element)
However, it doesn't mention what to do when there is an entirely unrelated problem caused by e.g. network or disk IO problems.
Simply throwing SQLException in next() and hasNext() is not possible because it is incompatible with the Iterator interface.
Here is my current code (simplified):
public class MyRecordIterator implements Iterator<Record>
{
private final ResultSet rs;
public MyRecordIterator() throws SQLException
{
rs = getConnection().createStatement().executeQuery(
"SELECT * FROM table");
}
@Override
public boolean hasNext()
{
try
{
return !rs.isAfterLast();
}
catch (SQLException e)
{
// ignore, hasNext() can't throw SQLException
}
}
@Override
public Record next()
{
try
{
if (rs.isAfterLast()) throw new NoSuchElementException();
rs.next();
Record result = new Record (rs.getString("column 1"), rs.getString("column 2")));
return result;
}
catch (SQLException e)
{
// ignore, next() can't throw SQLException
}
}
@Override
public void remove()
{
throw new UnsupportedOperationException("Iterator is read-only");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将检查的异常包装在未检查的异常中,允许在不破坏迭代器的情况下抛出它。
我建议使用特定于应用程序的异常来扩展 RuntimeException,实现构造函数(String、Throwable),以便您可以保留对原因的访问。
例如。
更新:要开始寻找更多信息,请尝试谷歌搜索“checked unchecked java sqlexception”。 'Best 上对检查与非检查异常处理进行了相当详细的讨论onjava.com 上的“异常处理实践” 以及 IBM Developerworks。
I would wrap the checked exception in an unchecked exception, allowing it to be thrown without breaking Iterator.
I'd suggest an application specific exception extending RuntimeException, implementing the constructor (String, Throwable) so that you can retain access to the cause.
eg.
Update: To get started looking for more info, try Googling 'checked unchecked java sqlexception'. Quite a detailed discussion of of checked vs. unchecked exception handling on 'Best Practises for Exception Handling' on onjava.com and discussion with some different approaches on IBM Developerworks.