开发者

Cannot implement try/catch/finally when variable is final

I have a problem.

I am using a variable which has to be final because I am using it in a anonymous inner class.

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

Problem is that I get a compiler error saying searcher cannot be resolved

And if I move the searcher up like so:

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

Then I get compi开发者_如何学运维le error saying searcher may not be initialized.

How can I fix this?

PS: I cannot use Lombok @Cleanup because the field has to be final for the anonymous inner class to work


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 {
}


It is a bit ugly, but I think this will do the trick;

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

and replace searcher with finalSearcher in the anonymous inner class.


put the body of the try{} block in another method:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜