test/tools/javac/TryWithResources/TwrMultiCatch.java

Mon, 26 Oct 2015 13:23:30 -0700

author
asaha
date
Mon, 26 Oct 2015 13:23:30 -0700
changeset 2999
683b3e7e05a7
parent 840
7f8794f9cc14
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u76-b00 for changeset 10ffafaf5340

darcy@609 1 /*
darcy@840 2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
darcy@609 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
darcy@609 4 *
darcy@609 5 * This code is free software; you can redistribute it and/or modify it
darcy@609 6 * under the terms of the GNU General Public License version 2 only, as
darcy@609 7 * published by the Free Software Foundation.
darcy@609 8 *
darcy@609 9 * This code is distributed in the hope that it will be useful, but WITHOUT
darcy@609 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
darcy@609 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
darcy@609 12 * version 2 for more details (a copy is included in the LICENSE file that
darcy@609 13 * accompanied this code).
darcy@609 14 *
darcy@609 15 * You should have received a copy of the GNU General Public License version
darcy@609 16 * 2 along with this work; if not, write to the Free Software Foundation,
darcy@609 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
darcy@609 18 *
darcy@609 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
darcy@609 20 * or visit www.oracle.com if you need additional information or have any
darcy@609 21 * questions.
darcy@609 22 */
darcy@609 23
darcy@609 24 /*
darcy@609 25 * @test
darcy@840 26 * @bug 6911256 6964740 7013420
darcy@609 27 * @author Joseph D. Darcy
darcy@609 28 * @summary Test that TWR and multi-catch play well together
darcy@609 29 * @compile TwrMultiCatch.java
darcy@609 30 * @run main TwrMultiCatch
darcy@609 31 */
darcy@609 32
darcy@609 33 import java.io.IOException;
darcy@609 34 public class TwrMultiCatch implements AutoCloseable {
darcy@609 35 private final Class<? extends Exception> exceptionClass;
darcy@609 36
darcy@609 37 private TwrMultiCatch(Class<? extends Exception> exceptionClass) {
darcy@609 38 this.exceptionClass = exceptionClass;
darcy@609 39 }
darcy@609 40
darcy@609 41 public static void main(String... args) {
darcy@609 42 test(new TwrMultiCatch(CustomCloseException1.class),
darcy@609 43 CustomCloseException1.class);
darcy@609 44
darcy@609 45 test(new TwrMultiCatch(CustomCloseException2.class),
darcy@609 46 CustomCloseException2.class);
darcy@609 47 }
darcy@609 48
darcy@609 49 private static void test(TwrMultiCatch twrMultiCatch,
darcy@609 50 Class<? extends Exception> expected) {
darcy@840 51 try(TwrMultiCatch tmc = twrMultiCatch) {
darcy@840 52 System.out.println(tmc.toString());
darcy@840 53 } catch (CustomCloseException1 |
darcy@609 54 CustomCloseException2 exception) {
darcy@609 55 if (!exception.getClass().equals(expected) ) {
darcy@609 56 throw new RuntimeException("Unexpected catch!");
darcy@609 57 }
darcy@609 58 }
darcy@609 59 }
darcy@609 60
darcy@609 61 public void close() throws CustomCloseException1, CustomCloseException2 {
darcy@609 62 Throwable t;
darcy@609 63 try {
darcy@609 64 t = exceptionClass.newInstance();
darcy@609 65 } catch(ReflectiveOperationException rfe) {
darcy@609 66 throw new RuntimeException(rfe);
darcy@609 67 }
darcy@609 68
darcy@609 69 try {
darcy@609 70 throw t;
darcy@840 71 } catch (CustomCloseException1 |
darcy@609 72 CustomCloseException2 exception) {
darcy@609 73 throw exception;
darcy@609 74 } catch (Throwable throwable) {
darcy@609 75 throw new RuntimeException(throwable);
darcy@609 76 }
darcy@609 77 }
darcy@609 78 }
darcy@609 79
darcy@609 80 class CustomCloseException1 extends Exception {}
darcy@609 81 class CustomCloseException2 extends Exception {}

mercurial