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