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

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

author
darcy
date
Mon, 02 Aug 2010 13:35:39 -0700
changeset 622
38e2c23309f1
parent 621
077eb94c912d
child 623
6318230cdb82

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

test/tools/javac/TryWithResources/TwrSuppression.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/TryWithResources/TwrSuppression.java	Mon Aug 02 13:35:39 2010 -0700
     1.3 @@ -0,0 +1,67 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6971877
    1.30 + * @author Joseph D. Darcy
    1.31 + * @summary Verify a primary exception suppresses all throwables
    1.32 + */
    1.33 +
    1.34 +public class TwrSuppression implements AutoCloseable {
    1.35 +    public static void main(String... args) throws Throwable {
    1.36 +        try {
    1.37 +            try (TwrSuppression r1 = new TwrSuppression(false);
    1.38 +                 TwrSuppression r2 = new TwrSuppression(true)) {
    1.39 +                throw new RuntimeException();
    1.40 +            }
    1.41 +        } catch(RuntimeException e) {
    1.42 +            Throwable[] suppressedExceptions = e.getSuppressedExceptions();
    1.43 +            int length = suppressedExceptions.length;
    1.44 +            if (length != 2)
    1.45 +                throw new RuntimeException("Unexpected length " + length);
    1.46 +
    1.47 +            if (suppressedExceptions[0].getClass() != Error.class ||
    1.48 +                suppressedExceptions[1].getClass() != Exception.class) {
    1.49 +                System.err.println("Unexpected suppressed types!");
    1.50 +                e.printStackTrace();
    1.51 +                throw new RuntimeException(e);
    1.52 +            }
    1.53 +        }
    1.54 +    }
    1.55 +
    1.56 +    private boolean throwError;
    1.57 +
    1.58 +    private TwrSuppression(boolean throwError) {
    1.59 +        this.throwError = throwError;
    1.60 +    }
    1.61 +
    1.62 +    @Override
    1.63 +    public void close() throws Exception {
    1.64 +        if (throwError) {
    1.65 +            throw new Error();
    1.66 +        } else {
    1.67 +            throw new Exception();
    1.68 +        }
    1.69 +    }
    1.70 +}

mercurial