繁体   English   中英

当变量为final时,无法实现try / catch / finally

[英]Cannot implement try/catch/finally when variable is final

我有个问题。

我使用的变量必须是final,因为我在匿名内部类中使用了它。

try { 
    final IndexSearcher searcher = new IndexSearcher(index.getDirectory(),true);

    searcher.search(query, new HitCollector() {
                public void collect(int doc, float score) {
                    try {
                        resultWorker.add(new ProcessDocument(searcher.doc(doc)));
                    } catch (CorruptIndexException e) {
                        log.error("Corrupt index found during search", e);
                    } catch (IOException e) {
                        log.error("Error during search", e);
                    }
                }
            });
} catch (CorruptIndexException e) {
    log.error("Corrupt index found during search", e);
} catch (IOException e) {
    log.error("Error during search", e);
} finally {
    if(searcher != null) 
        searcher.close();
}

问题是我收到一个编译器错误,提示searcher cannot be resolved

如果我像这样向上移动搜索器:

final IndexSearcher searcher;
try {
    searcher = new IndexSearcher(index.getDirectory(),true);

然后出现编译错误,提示searcher may not be initialized

我怎样才能解决这个问题?

PS:我不能使用Lombok @Cleanup,因为该字段对于匿名内部类来说必须是最终的才能起作用

try { 
    // if new IndexSearcher throws, searcher will not be initialized, and doesn't need a close. The catch below takes care of reporting the error.
    final IndexSearcher searcher = new IndexSearcher(index.getDirectory(),true);
    try {
        searcher.search(query, new HitCollector() {
                public void collect(int doc, float score) {
                    try {
                        resultWorker.add(new ProcessDocument(searcher.doc(doc)));
                    } catch (CorruptIndexException e) {
                        log.error("Corrupt index found during search", e);
                    } catch (IOException e) {
                        log.error("Error during search", e);
                    }
                }
            });
    } finally {
        searcher.close();
    }
} catch (CorruptIndexException e) {
    log.error("Corrupt index found during search", e);
} catch (IOException e) {
    log.error("Error during search", e);
} finally {
}

这有点丑陋,但是我认为这可以解决问题。

IndexSearcher searcher = null; 
try { 
    searcher = new IndexSearcher(index.getDirectory(), true);
    final IndexSearcher finalSearcher = searcher;

并在匿名内部类finalSearcher searcher替换为finalSearcher

将try {}块的主体放入另一种方法中:

  IndexSearch searcher = openSearcher();
  try {
    doSearch(searcher, query, resultWorker);
  } finally {
    searcher.close();
  }

private void doSearch(final IndexSearcher searcher, Query query, final ResultWorker resultWorker) {
  searcher.search(new HitCollector() {
    public void collect(int doc, float score) {
      resultWorker.add(new ProcessDocument(searcher.doc(doc));
    }
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM