test/tools/javac/multicatch/Pos11.java

changeset 1756
53b389eb39c1
parent 0
959103a6100f
equal deleted inserted replaced
1755:ddb4a2bfcd82 1756:53b389eb39c1
1 /*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8013163
27 * @author sogoel
28 * @summary Test multiple nested multi-catch blocks with Exception hierarchies
29 * @run main Pos11
30 */
31
32 /*
33 * For this test, exception hierarchy used:
34 *
35 * Throwable
36 * / \
37 * Exception Error
38 * | | |
39 * A B D
40 * |
41 * C
42 * |
43 * E
44 * As an exception is thrown within a nested try-catch block, outer catch blocks
45 * will catch an exception or its child exceptions, so the same exception can
46 * be caught and rethrown multiple times.
47 */
48
49 public class Pos11 {
50
51 public static String results = "";
52 public static String sExpected = "-AB:A-AB:B-CD:C-AB:C-CD:D-Throwable:D-CD:E" +
53 "-AB:E-Exception:Exception-Throwable:Exception";
54
55 enum TestExceptions {
56 A("A"),
57 B("B"),
58 C("C"),
59 D("D"),
60 E("E"),
61 U("U");
62
63 String exType;
64 TestExceptions(String type) {
65 this.exType = type;
66 }
67 }
68
69 public static void main(String... args) {
70 Pos11 pos11 = new Pos11();
71 for(TestExceptions t : TestExceptions.values()) {
72 pos11.rethrower(t.exType);
73 }
74 if (results.compareTo(sExpected) != 0)
75 throw new RuntimeException("FAIL: final strings did not match:\n"
76 + results + "!=\n" + sExpected);
77 System.out.println("PASS");
78 }
79
80 void rethrower(String T) {
81 try { /* try1 */
82 try { /* try2 */
83 try { /* try3 */
84 try { /* try4 */
85 switch (T) {
86 case "A":
87 throw new A();
88 case "B":
89 throw new B();
90 case "C":
91 throw new C();
92 case "D":
93 throw new D();
94 case "E":
95 throw new E();
96 default:
97 throw new Exception(
98 new Throwable());
99 }
100 } catch ( final C|D cd) {
101 results=results.concat("-CD:" + cd.getClass().getSimpleName());
102 throw cd;
103 }
104 } catch (final A|B ab) {
105 results=results.concat("-AB:" + ab.getClass().getSimpleName());
106 }
107 } catch (final Exception e ) {
108 results=results.concat("-Exception:" + e.getClass().getSimpleName());
109 throw e;
110 }
111 } catch (Throwable t) {
112 results=results.concat("-Throwable:" + t.getClass().getSimpleName());
113 }
114 }
115
116 // Test Exception
117 static class A extends Exception {}
118
119 // Test Exception
120 static class B extends Exception {}
121
122 // Test Exception
123 static class C extends B {}
124
125 // Not a descendant of Exception
126 static class D extends Error {}
127
128 // Test Exception
129 static class E extends C {}
130
131 }
132

mercurial