test/tools/javac/multicatch/Pos04.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

     1 /*
     2  * Copyright (c) 2010, 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  */
    24 /*
    25  * @test
    26  * @bug 6943289
    27  * @summary Project Coin: Improved Exception Handling for Java (aka 'multicatch')
    28  */
    30 import java.lang.annotation.*;
    32 public class Pos04 {
    34     enum ExceptionKind {
    35         A(1),
    36         B(2),
    37         C(1);
    39         int expectedValue;
    41         ExceptionKind(int expectedValue) {
    42             this.expectedValue = expectedValue;
    43         }
    44     }
    46     @Retention(RetentionPolicy.RUNTIME)
    47     @interface CatchNumber {
    48        int value();
    49     }
    51     @CatchNumber(1)
    52     static class A extends Exception { }
    54     @CatchNumber(2)
    55     static class B extends Exception { }
    57     @CatchNumber(1)
    58     static class C extends Exception { }
    60     static int sum = 0;
    62     public static void main(String[] args) {
    63         for (ExceptionKind ekind : ExceptionKind.values()) {
    64             test(ekind);
    65         }
    66         if (sum != 4) {
    67             throw new Error("bad checksum - expected:4, found:" + sum);
    68         }
    69     }
    71     public static void test(ExceptionKind ekind) {
    72         try {
    73             switch(ekind) {
    74                 case A: throw new A();
    75                 case B: throw new B();
    76                 case C: throw new C();
    77             }
    78         } catch(final A | C ex) {// Catch number 1
    79             CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class);
    80             if (catchNumber == null || catchNumber.value() != ekind.expectedValue) {
    81                 throw new Error("was expecting 1 - got " + catchNumber);
    82             }
    83             sum += catchNumber.value();
    84         } catch (final B ex) { // Catch number 2
    85             CatchNumber catchNumber = ex.getClass().getAnnotation(CatchNumber.class);
    86             if (catchNumber == null || catchNumber.value() != ekind.expectedValue) {
    87                 throw new Error("was expecting 2 - got " + catchNumber);
    88             }
    89             sum += catchNumber.value();
    90         }
    91     }
    92 }

mercurial