Here you can find the source of close(final Exception e, final Closeable... objects)
Parameter | Description |
---|---|
objects | objects to close |
public static void close(final Exception e, final Closeable... objects) throws IOException
//package com.java2s; /*//from w ww. jav a 2s .c om * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.Closeable; import java.io.IOException; import java.util.Arrays; public class Main { /** * Closes all given {@link Closeable}s. Some of the {@linkplain Closeable}s may be null; they are * ignored. After everything is closed, the method either throws the first exception it hit * while closing with other exceptions added as suppressed, or completes normally if there were * no exceptions. * * @param objects objects to close */ public static void close(final Closeable... objects) throws IOException { close(null, Arrays.asList(objects)); } /** * Closes all given {@link Closeable}s. Some of the {@linkplain Closeable}s may be null; they are * ignored. After everything is closed, the method adds any exceptions as suppressed to the * original exception, or throws the first exception it hit if {@code Exception} is null. If * no exceptions are encountered and the passed in exception is null, it completes normally. * * @param objects objects to close */ public static void close(final Exception e, final Closeable... objects) throws IOException { close(e, Arrays.asList(objects)); } /** * Closes all given {@link Closeable}s. Some of the {@linkplain Closeable}s may be null; they are * ignored. After everything is closed, the method either throws the first exception it hit * while closing with other exceptions added as suppressed, or completes normally if there were * no exceptions. * * @param objects objects to close */ public static void close(final Iterable<? extends Closeable> objects) throws IOException { close(null, objects); } /** * Closes all given {@link Closeable}s. If a non-null exception is passed in, or closing a * stream causes an exception, throws the exception with other {@link RuntimeException} or * {@link IOException} exceptions added as suppressed. * * @param ex existing Exception to add exceptions occurring during close to * @param objects objects to close * * @see #close(Closeable...) */ public static void close(final Exception ex, final Iterable<? extends Closeable> objects) throws IOException { Exception firstException = ex; for (final Closeable object : objects) { try { if (object != null) { object.close(); } } catch (final IOException | RuntimeException e) { if (firstException == null) { firstException = e; } else { firstException.addSuppressed(e); } } } if (firstException != null) { if (firstException instanceof IOException) { throw (IOException) firstException; } else { // since we only assigned an IOException or a RuntimeException to ex above, in this case ex must be a RuntimeException throw (RuntimeException) firstException; } } } }