With our internal Lucene IOUtils it's even simplier, see javadocs :-) This
is why I proposed to use it also for TIKA:

 Closeable resource1 = null, resource2 = null, resource3 = null;
 ExpectedException priorE = null;
 try {
   resource1 = ...; resource2 = ...; resource3 = ...; // Acquisition may
throw ExpectedException
   ..do..stuff.. // May throw ExpectedException
 } catch (ExpectedException e) {
   priorE = e;
 } finally {
   IOUtils.closeSafely(priorE, resource1, resource2, resource3);
 }

The above code is identical to the Java7 try-with-resources (100% identical
behaviour, if Java7 is detected it will also log suppressed exceptions to
the priorE stack trace). It's just a few lines more code.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: [email protected]


> -----Original Message-----
> From: Michael McCandless [mailto:[email protected]]
> Sent: Thursday, September 01, 2011 12:18 PM
> To: [email protected]
> Subject: Re: Tika leaves files open
> 
> On Thu, Sep 1, 2011 at 5:56 AM, Jukka Zitting <[email protected]>
> wrote:
> > Hi,
> >
> > On Thu, Sep 1, 2011 at 11:36 AM, Michael McCandless
> > <[email protected]> wrote:
> >> While rare for IS.close() to throw an exception, if it does, it's
> >> quite awful because it masks the original exception.  It seems best
> >> to be defensive?
> >
> > With closeQuietly() you'd really be throwing information out in case
> > where close() fails when no other exception has been thrown. Instead
> > of one exception masking another, you'd have no exceptions masking
> > one!
> 
> Duh, you're right: we don't want to use closeQuietly if there was no
exception.
> 
> For Lucene we do this:
> 
>   // open something
>   boolean success = false;
>   try {
>      // do something
>     success = true;
>   } finally {
>     if (!success) {
>       closeQuietly();
>     } else {
>       closeNormally();
>     }
>   }
> 
> This gets cleaner with Java 7 but it's some ways away before Tika can
require
> Java 7...
> 
> Mike

Reply via email to