commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sebb (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (IO-500) IOUtils: plz include close* functions from lucene util's IOUtils class
Date Thu, 10 Mar 2016 20:30:41 GMT

    [ https://issues.apache.org/jira/browse/IO-500?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15189892#comment-15189892
] 

Sebb commented on IO-500:
-------------------------

It's not safe to catch and ignore Throwable. The code should only catch IOException.

There is already a closeQuietly(Closeable) method:

http://commons.apache.org/proper/commons-io/javadocs/api-2.4/org/apache/commons/io/IOUtils.html#closeQuietly%28java.io.Closeable%29

That could perhaps be overloaded to accept an Iterable.

> IOUtils: plz include close* functions from lucene util's IOUtils class
> ----------------------------------------------------------------------
>
>                 Key: IO-500
>                 URL: https://issues.apache.org/jira/browse/IO-500
>             Project: Commons IO
>          Issue Type: Improvement
>          Components: Utilities
>            Reporter: Mark
>            Priority: Minor
>
> {code}
>   /**
>    * Closes all given <tt>Closeable</tt>s.  Some of the
>    * <tt>Closeable</tt>s may be null; they are
>    * ignored.  After everything is closed, the method either
>    * throws the first exception it hit while closing, or
>    * completes normally if there were no exceptions.
>    * 
>    * @param objects
>    *          objects to call <tt>close()</tt> on
>    */
>   public static void close(Closeable... objects) throws IOException {
>     close(Arrays.asList(objects));
>   }
>   
>   /**
>    * Closes all given <tt>Closeable</tt>s.
>    * @see #close(Closeable...)
>    */
>   public static void close(Iterable<? extends Closeable> objects) throws IOException
{
>     Throwable th = null;
>     for (Closeable object : objects) {
>       try {
>         if (object != null) {
>           object.close();
>         }
>       } catch (Throwable t) {
>         addSuppressed(th, t);
>         if (th == null) {
>           th = t;
>         }
>       }
>     }
>     reThrow(th);
>   }
>   /**
>    * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
>    * Some of the <tt>Closeable</tt>s may be null, they are ignored.
>    * 
>    * @param objects
>    *          objects to call <tt>close()</tt> on
>    */
>   public static void closeWhileHandlingException(Closeable... objects) {
>     closeWhileHandlingException(Arrays.asList(objects));
>   }
>   
>   /**
>    * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions.
>    * @see #closeWhileHandlingException(Closeable...)
>    */
>   public static void closeWhileHandlingException(Iterable<? extends Closeable>
objects) {
>     for (Closeable object : objects) {
>       try {
>         if (object != null) {
>           object.close();
>         }
>       } catch (Throwable t) {
>       }
>     }
>   }
>   
>   /** adds a Throwable to the list of suppressed Exceptions of the first Throwable
>    * @param exception this exception should get the suppressed one added
>    * @param suppressed the suppressed exception
>    */
>   private static void addSuppressed(Throwable exception, Throwable suppressed) {
>     if (exception != null && suppressed != null) {
>       exception.addSuppressed(suppressed);
>     }
>   }
>   {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message