langtools/test/tools/javac/multicatch/Pos02.java
changeset 5492 515e4b33b335
child 5520 86e4b9a9da40
equal deleted inserted replaced
5491:899e1748bb17 5492:515e4b33b335
       
     1 /*
       
     2  * Copyright 2010 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 6943289
       
    27  * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch')
       
    28  *
       
    29  */
       
    30 
       
    31 public class Pos02 {
       
    32 
       
    33     static class A extends Exception {}
       
    34     static class B extends Exception {}
       
    35     static class C extends Exception {}
       
    36     static class C1 extends C {}
       
    37     static class C2 extends C {}
       
    38 
       
    39     enum ExceptionKind {
       
    40         A,
       
    41         B,
       
    42         C1,
       
    43         C2
       
    44     }
       
    45 
       
    46     static int caughtExceptions = 0;
       
    47     static int caughtRethrownExceptions = 0;
       
    48 
       
    49     static void test(ExceptionKind ekind) throws A, C1 {
       
    50         try {
       
    51             switch (ekind) {
       
    52                 case A : throw new A();
       
    53                 case B : throw new B();
       
    54                 case C1: throw new C1();
       
    55                 case C2 : throw new C2();
       
    56             }
       
    57         }
       
    58         catch (final C2 | B ex) {
       
    59             caughtExceptions++;
       
    60         }
       
    61         catch (final C | A ex) {
       
    62             caughtExceptions++;
       
    63             throw ex;
       
    64         }
       
    65     }
       
    66 
       
    67     public static void main(String[] args) {
       
    68         for (ExceptionKind ekind : ExceptionKind.values()) {
       
    69             try {
       
    70                 test(ekind);
       
    71             }
       
    72             catch (final C1 | A ex) {
       
    73                 caughtRethrownExceptions++;
       
    74             }
       
    75         }
       
    76         if (caughtExceptions != 4 && caughtRethrownExceptions == 2) {
       
    77             throw new AssertionError("Exception handler called " + caughtExceptions + "times" +
       
    78                                      " rethrown handler called " + caughtRethrownExceptions + "times");
       
    79         }
       
    80     }
       
    81 }