Java Stream Close closeAll(Exception firstEx, Closeable... objects)

Here you can find the source of closeAll(Exception firstEx, Closeable... objects)

Description

Same as closeAll(Closeable...) but allows passing an initial exception, when one may have been thrown earlier during a shutdown procedure.

License

Open Source License

Declaration

public static void closeAll(Exception firstEx, Closeable... objects) throws Exception 

Method Source Code

//package com.java2s;
/*-/*from ww w . j  a  va  2s  .c o m*/
 *
 *  This file is part of Oracle Berkeley DB Java Edition
 *  Copyright (C) 2002, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 *  Oracle Berkeley DB Java Edition is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, version 3.
 *
 *  Oracle Berkeley DB Java Edition is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License in
 *  the LICENSE file along with Oracle Berkeley DB Java Edition.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 *  An active Oracle commercial licensing agreement for this product
 *  supercedes this license.
 *
 *  For more information please contact:
 *
 *  Vice President Legal, Development
 *  Oracle America, Inc.
 *  5OP-10
 *  500 Oracle Parkway
 *  Redwood Shores, CA 94065
 *
 *  or
 *
 *  berkeleydb-info_us@oracle.com
 *
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  [This line intentionally left blank.]
 *  EOF
 *
 */

import java.io.Closeable;

public class Main {
    /**
     * Calls Closeable.close for each parameter in the order given, if it is
     * non-null.
     * 
     * If one or more close methods throws an Exception, all close methods will
     * still be called and the first Exception will be rethrown.  If an Error
     * is thrown by a close method, it will be thrown by this method and no
     * further close methods will be called.  An IOException may be thrown by a
     * close method because is declared by Closeable.close; however, the use of
     * RuntimeExceptions is recommended.
     */
    public static void closeAll(Closeable... objects) throws Exception {

        closeAll(null, objects);
    }

    /**
     * Same as closeAll(Closeable...) but allows passing an initial exception,
     * when one may have been thrown earlier during a shutdown procedure.  If
     * null is passed for the firstEx parameter, calling this method is
     * equivalent to calling closeAll(Closeable...).
     */
    public static void closeAll(Exception firstEx, Closeable... objects) throws Exception {

        for (Closeable c : objects) {
            if (c == null) {
                continue;
            }
            try {
                c.close();
            } catch (Exception e) {
                if (firstEx == null) {
                    firstEx = e;
                }
            }
        }

        if (firstEx != null) {
            throw firstEx;
        }
    }
}

Related

  1. close(ZipFile file)
  2. close(ZipFile zipFile)
  3. closeAll()
  4. closeAll()
  5. closeAll(Collection closeables)
  6. closeAll(Iterable closeables)
  7. closeAll(T... io)
  8. closeAllStreamsQuietly(final Closeable... closeables)
  9. closeAndRethrow(final Exception e, final Closeable... channels)