Closes all Closeable instances that have been added to this Closer. If an exception was thrown in the try block and passed to one of the exceptionThrown methods, any exceptions thrown when attempting to close a closeable will be suppressed. Otherwise, the first excepti
()
| 193 | * additional exceptions that are thrown after that will be suppressed. |
| 194 | */ |
| 195 | @Override |
| 196 | public void close() throws IOException { |
| 197 | Throwable throwable = thrown; |
| 198 | |
| 199 | // close closeables in LIFO order |
| 200 | while (!stack.isEmpty()) { |
| 201 | Closeable closeable = stack.removeFirst(); |
| 202 | try { |
| 203 | closeable.close(); |
| 204 | } catch (Throwable e) { |
| 205 | if (throwable == null) { |
| 206 | throwable = e; |
| 207 | } else { |
| 208 | suppressor.suppress(closeable, throwable, e); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (thrown == null && throwable != null) { |
| 214 | throwIfInstanceOf(throwable, IOException.class); |
| 215 | throwIfUnchecked(throwable); |
| 216 | throw new AssertionError(throwable); // not possible |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** Suppression strategy interface. */ |
| 221 | @VisibleForTesting |