迭代器实现如何处理检查的异常? [英] How should Iterator implementation deal with checked exceptions?

查看:121
本文介绍了迭代器实现如何处理检查的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在java.sql.RecordSet中包装一个java.util.Iterator。我的问题是,如果任何记录集方法抛出SQLException,我应该怎么办?



java.util.Iterator javadoc 解释了在各种情况下抛出的异常(即如果您调用next(),则为NoSuchElementException)超过最后一个元素)



但是,当没有完全不相关的问题时网络或磁盘IO问题。



只有在next()和hasNext()中抛出SQLException是不可能的,因为它与Iterator接口不兼容。



这是我现在的代码(简化):

  public class MyRecordIterator实现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)
{
//忽略,hasNext()不能抛出SQLException
}
}

@Override
public Record next()
{
try
{
if(rs.isAfterLast())throw new NoSuchElementException();
rs.next();
记录结果=新记录(rs.getString(column 1),rs.getString(column 2)));
返回结果;
}
catch(SQLException e)
{
// ignore,next()不能抛出SQLException
}
}

@Override
public void remove()
{
throw new UnsupportedOperationException(Iterator is read-only);
}
}


解决方案

将被检查的异常包装在一个未检查的异常中,允许它抛出而不会破坏Iterator。



我建议一个应用程序特定的异常扩展RuntimeException,实现构造函数(String ,Throwable),以便您可以保留访问原因。



例如。

  @Override 
public boolean hasNext(){
try {
return!rs.isAfterLast();
} catch(SQLException e){
throw new MyApplicationException(有一个错误,e);
}
}






更新:要开始寻找更多信息,请尝试使用Google搜索未检查的java sqlexception。关于最佳的有关检查和未检查异常处理的详细讨论onjava.com上的异常处理实践,以及有关 IBM Developerworks


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");
    }
}

解决方案

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.

    @Override
    public boolean hasNext() {
      try {
        return !rs.isAfterLast();
      } catch (SQLException e) {
        throw new MyApplicationException("There was an error", e);
      }
    }


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.

这篇关于迭代器实现如何处理检查的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆