title: How can I check if a Lucene IndexWriter instance is valid/open?
body: <p>Sorry for the simple question, but there doesn't seem to be any obvious way. According to the documentation, it is recommended to keep a single instance of <code>IndexWriter</code> in memory that can be used again and again for updates, as opposed to opening and closing one for each change (which is much more costly).</p>
  
  <p>However, the documentation also states that the <code>IndexWriter</code> should be closed if an exception occurs (e.g. <code>OutOfMemoryException</code>).</p>
  
  <p>Therefore, I need some way to check if my instance of <code>IndexWriter</code> is valid or not, assuming it could be closed by some other operation.</p>
  
  <p>I realize I will get an exception if I try to use it when it's closed, but rather than fail, I would like to check to see if I need to create a new <code>IndexWriter</code> before I need it so no failure occurs.</p>
  
  <p>It seems to me a simple <code>IsOpen</code> property/method would be such an obvious addition.</p>
  
  <p><strong>Thoughts:</strong></p>
  
  <p>I suppose that if I could ensure that my class is the only thing capable of closing the <code>IndexWriter</code>, I could simply set it to <code>null</code> when it was closed and just check to make sure it's not <code>null</code> whenever I go to use it, however this would not handle cases where the <code>IndexWriter</code> closes itself (if such a case is possible now or in the future).</p>
  
  <p>Another thought is that I could wrap an attempt to use it in a try/catch block and use the exception to indicate that it needs to be reopened, but this doesn't seem very efficient/elegant. What method would I use to test the validity of the <code>IndexWriter</code>?</p>