aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 8013163 aoqi@0: * @author sogoel aoqi@0: * @summary Test multiple nested multi-catch blocks with Exception hierarchies aoqi@0: * @run main Pos11 aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * For this test, exception hierarchy used: aoqi@0: * aoqi@0: * Throwable aoqi@0: * / \ aoqi@0: * Exception Error aoqi@0: * | | | aoqi@0: * A B D aoqi@0: * | aoqi@0: * C aoqi@0: * | aoqi@0: * E aoqi@0: * As an exception is thrown within a nested try-catch block, outer catch blocks aoqi@0: * will catch an exception or its child exceptions, so the same exception can aoqi@0: * be caught and rethrown multiple times. aoqi@0: */ aoqi@0: aoqi@0: public class Pos11 { aoqi@0: aoqi@0: public static String results = ""; aoqi@0: public static String sExpected = "-AB:A-AB:B-CD:C-AB:C-CD:D-Throwable:D-CD:E" + aoqi@0: "-AB:E-Exception:Exception-Throwable:Exception"; aoqi@0: aoqi@0: enum TestExceptions { aoqi@0: A("A"), aoqi@0: B("B"), aoqi@0: C("C"), aoqi@0: D("D"), aoqi@0: E("E"), aoqi@0: U("U"); aoqi@0: aoqi@0: String exType; aoqi@0: TestExceptions(String type) { aoqi@0: this.exType = type; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public static void main(String... args) { aoqi@0: Pos11 pos11 = new Pos11(); aoqi@0: for(TestExceptions t : TestExceptions.values()) { aoqi@0: pos11.rethrower(t.exType); aoqi@0: } aoqi@0: if (results.compareTo(sExpected) != 0) aoqi@0: throw new RuntimeException("FAIL: final strings did not match:\n" aoqi@0: + results + "!=\n" + sExpected); aoqi@0: System.out.println("PASS"); aoqi@0: } aoqi@0: aoqi@0: void rethrower(String T) { aoqi@0: try { /* try1 */ aoqi@0: try { /* try2 */ aoqi@0: try { /* try3 */ aoqi@0: try { /* try4 */ aoqi@0: switch (T) { aoqi@0: case "A": aoqi@0: throw new A(); aoqi@0: case "B": aoqi@0: throw new B(); aoqi@0: case "C": aoqi@0: throw new C(); aoqi@0: case "D": aoqi@0: throw new D(); aoqi@0: case "E": aoqi@0: throw new E(); aoqi@0: default: aoqi@0: throw new Exception( aoqi@0: new Throwable()); aoqi@0: } aoqi@0: } catch ( final C|D cd) { aoqi@0: results=results.concat("-CD:" + cd.getClass().getSimpleName()); aoqi@0: throw cd; aoqi@0: } aoqi@0: } catch (final A|B ab) { aoqi@0: results=results.concat("-AB:" + ab.getClass().getSimpleName()); aoqi@0: } aoqi@0: } catch (final Exception e ) { aoqi@0: results=results.concat("-Exception:" + e.getClass().getSimpleName()); aoqi@0: throw e; aoqi@0: } aoqi@0: } catch (Throwable t) { aoqi@0: results=results.concat("-Throwable:" + t.getClass().getSimpleName()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Test Exception aoqi@0: static class A extends Exception {} aoqi@0: aoqi@0: // Test Exception aoqi@0: static class B extends Exception {} aoqi@0: aoqi@0: // Test Exception aoqi@0: static class C extends B {} aoqi@0: aoqi@0: // Not a descendant of Exception aoqi@0: static class D extends Error {} aoqi@0: aoqi@0: // Test Exception aoqi@0: static class E extends C {} aoqi@0: aoqi@0: } aoqi@0: