/** * 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; } } } if (th != null) { if (th instanceof IOException) throw (IOException) th; if (th instanceof RuntimeException) throw (RuntimeException) th; if (th instanceof Error) throw (Error) th; throw new RuntimeException(th); } }
/** * Closes all given <tt>Closeable</tt>s, suppressing all thrown exceptions. * @see #closeWhileHandlingException(Exception, Closeable...) */ public static <E extends Exception> void closeWhileHandlingException(E priorException, Iterable<? extends Closeable> objects) throws E, IOException { Throwable th = null; for (Closeable object : objects) { try { if (object != null) { object.close(); } } catch (Throwable t) { addSuppressed((priorException == null) ? th : priorException, t); if (th == null) { th = t; } } } if (priorException != null) { throw priorException; } else if (th != null) { if (th instanceof IOException) throw (IOException) th; if (th instanceof RuntimeException) throw (RuntimeException) th; if (th instanceof Error) throw (Error) th; throw new RuntimeException(th); } }
addSuppressed(th, t); if (th == null) { th = t;
/** * 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); }
addSuppressed((priorException == null) ? th : priorException, t); if (th == null) { th = t;
/** * 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); }
/** * Deletes all given <tt>Path</tt>s, if they exist. Some of the * <tt>File</tt>s may be null; they are * ignored. After everything is deleted, the method either * throws the first exception it hit while deleting, or * completes normally if there were no exceptions. * * @param files files to delete */ public static void deleteFilesIfExist(Collection<? extends Path> files) throws IOException { Throwable th = null; for (Path file : files) { try { if (file != null) { Files.deleteIfExists(file); } } catch (Throwable t) { addSuppressed(th, t); if (th == null) { th = t; } } } reThrow(th); }
/** * Deletes all given <tt>Path</tt>s, if they exist. Some of the * <tt>File</tt>s may be null; they are * ignored. After everything is deleted, the method either * throws the first exception it hit while deleting, or * completes normally if there were no exceptions. * * @param files files to delete */ public static void deleteFilesIfExist(Collection<? extends Path> files) throws IOException { Throwable th = null; for (Path file : files) { try { if (file != null) { Files.deleteIfExists(file); } } catch (Throwable t) { addSuppressed(th, t); if (th == null) { th = t; } } } reThrow(th); }