test/tools/javac/multicatch/Pos11.java

changeset 1756
53b389eb39c1
parent 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/multicatch/Pos11.java	Tue May 14 18:02:48 2013 -0700
     1.3 @@ -0,0 +1,132 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 2013, 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     8013163
    1.30 + * @author  sogoel
    1.31 + * @summary Test multiple nested multi-catch blocks with Exception hierarchies
    1.32 + * @run main Pos11
    1.33 + */
    1.34 +
    1.35 +/*
    1.36 + * For this test, exception hierarchy used:
    1.37 + *
    1.38 + *           Throwable
    1.39 + *             /   \
    1.40 + *      Exception  Error
    1.41 + *       |    |      |
    1.42 + *       A    B      D
    1.43 + *            |
    1.44 + *            C
    1.45 + *            |
    1.46 + *            E
    1.47 + * As an exception is thrown within a nested try-catch block, outer catch blocks
    1.48 + * will catch an exception or its child exceptions, so the same exception can
    1.49 + * be caught and rethrown multiple times.
    1.50 + */
    1.51 +
    1.52 +public class Pos11 {
    1.53 +
    1.54 +    public static String results = "";
    1.55 +    public static String sExpected = "-AB:A-AB:B-CD:C-AB:C-CD:D-Throwable:D-CD:E" +
    1.56 +            "-AB:E-Exception:Exception-Throwable:Exception";
    1.57 +
    1.58 +    enum TestExceptions {
    1.59 +        A("A"),
    1.60 +        B("B"),
    1.61 +        C("C"),
    1.62 +        D("D"),
    1.63 +        E("E"),
    1.64 +        U("U");
    1.65 +
    1.66 +        String exType;
    1.67 +        TestExceptions(String type) {
    1.68 +            this.exType = type;
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +    public static void main(String... args) {
    1.73 +        Pos11 pos11 = new Pos11();
    1.74 +        for(TestExceptions t : TestExceptions.values()) {
    1.75 +            pos11.rethrower(t.exType);
    1.76 +        }
    1.77 +        if (results.compareTo(sExpected) != 0)
    1.78 +            throw new RuntimeException("FAIL: final strings did not match:\n"
    1.79 +                    + results + "!=\n" + sExpected);
    1.80 +        System.out.println("PASS");
    1.81 +    }
    1.82 +
    1.83 +    void rethrower(String T) {
    1.84 +        try { /* try1 */
    1.85 +            try { /* try2 */
    1.86 +                try { /* try3 */
    1.87 +                    try { /* try4 */
    1.88 +                        switch (T) {
    1.89 +                        case "A":
    1.90 +                            throw new A();
    1.91 +                        case "B":
    1.92 +                            throw new B();
    1.93 +                        case "C":
    1.94 +                            throw new C();
    1.95 +                        case "D":
    1.96 +                            throw new D();
    1.97 +                        case "E":
    1.98 +                            throw new E();
    1.99 +                        default:
   1.100 +                            throw new Exception(
   1.101 +                                    new Throwable());
   1.102 +                        }
   1.103 +                    } catch ( final C|D cd) {
   1.104 +                        results=results.concat("-CD:" + cd.getClass().getSimpleName());
   1.105 +                        throw cd;
   1.106 +                    }
   1.107 +                } catch (final A|B ab) {
   1.108 +                    results=results.concat("-AB:" + ab.getClass().getSimpleName());
   1.109 +                }
   1.110 +            } catch (final Exception e ) {
   1.111 +                results=results.concat("-Exception:" + e.getClass().getSimpleName());
   1.112 +                throw e;
   1.113 +            }
   1.114 +        } catch (Throwable t) {
   1.115 +            results=results.concat("-Throwable:" + t.getClass().getSimpleName());
   1.116 +        }
   1.117 +    }
   1.118 +
   1.119 +    // Test Exception
   1.120 +    static class A extends Exception {}
   1.121 +
   1.122 +    // Test Exception
   1.123 +    static class B extends Exception {}
   1.124 +
   1.125 +    // Test Exception
   1.126 +    static class C extends B {}
   1.127 +
   1.128 +    // Not a descendant of Exception
   1.129 +    static class D extends Error {}
   1.130 +
   1.131 +    // Test Exception
   1.132 +    static class E extends C {}
   1.133 +
   1.134 +}
   1.135 +

mercurial