test/tools/javac/TryWithResources/TwrSuppression.java

Mon, 02 Aug 2010 13:35:39 -0700

author
darcy
date
Mon, 02 Aug 2010 13:35:39 -0700
changeset 622
38e2c23309f1
child 745
4328728e0409
permissions
-rw-r--r--

6971877: Project Coin: improve semantics of suppressed exceptions in try-with-resources
Reviewed-by: jjb

darcy@622 1 /*
darcy@622 2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
darcy@622 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@622 4 *
darcy@622 5 * This code is free software; you can redistribute it and/or modify it
darcy@622 6 * under the terms of the GNU General Public License version 2 only, as
darcy@622 7 * published by the Free Software Foundation.
darcy@622 8 *
darcy@622 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@622 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@622 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@622 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@622 13 * accompanied this code).
darcy@622 14 *
darcy@622 15 * You should have received a copy of the GNU General Public License version
darcy@622 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@622 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@622 18 *
darcy@622 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@622 20 * or visit www.oracle.com if you need additional information or have any
darcy@622 21 * questions.
darcy@622 22 */
darcy@622 23
darcy@622 24 /*
darcy@622 25 * @test
darcy@622 26 * @bug 6971877
darcy@622 27 * @author Joseph D. Darcy
darcy@622 28 * @summary Verify a primary exception suppresses all throwables
darcy@622 29 */
darcy@622 30
darcy@622 31 public class TwrSuppression implements AutoCloseable {
darcy@622 32 public static void main(String... args) throws Throwable {
darcy@622 33 try {
darcy@622 34 try (TwrSuppression r1 = new TwrSuppression(false);
darcy@622 35 TwrSuppression r2 = new TwrSuppression(true)) {
darcy@622 36 throw new RuntimeException();
darcy@622 37 }
darcy@622 38 } catch(RuntimeException e) {
darcy@622 39 Throwable[] suppressedExceptions = e.getSuppressedExceptions();
darcy@622 40 int length = suppressedExceptions.length;
darcy@622 41 if (length != 2)
darcy@622 42 throw new RuntimeException("Unexpected length " + length);
darcy@622 43
darcy@622 44 if (suppressedExceptions[0].getClass() != Error.class ||
darcy@622 45 suppressedExceptions[1].getClass() != Exception.class) {
darcy@622 46 System.err.println("Unexpected suppressed types!");
darcy@622 47 e.printStackTrace();
darcy@622 48 throw new RuntimeException(e);
darcy@622 49 }
darcy@622 50 }
darcy@622 51 }
darcy@622 52
darcy@622 53 private boolean throwError;
darcy@622 54
darcy@622 55 private TwrSuppression(boolean throwError) {
darcy@622 56 this.throwError = throwError;
darcy@622 57 }
darcy@622 58
darcy@622 59 @Override
darcy@622 60 public void close() throws Exception {
darcy@622 61 if (throwError) {
darcy@622 62 throw new Error();
darcy@622 63 } else {
darcy@622 64 throw new Exception();
darcy@622 65 }
darcy@622 66 }
darcy@622 67 }

mercurial