Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 // An error class.
     18 class BadError extends Error {
     19     public BadError(String s) {
     20         super("This is bad by convention: " + s);
     21     }
     22 }
     23 
     24 // A class that throws BadError during static initialization.
     25 class BadInit {
     26     static int dummy;
     27     static {
     28         System.out.println("Static Init");
     29         if (true) {
     30             throw new BadError("BadInit");
     31         }
     32     }
     33 }
     34 
     35 // An error that doesn't have a <init>(String) method.
     36 class BadErrorNoStringInit extends Error {
     37     public BadErrorNoStringInit() {
     38         super("This is bad by convention");
     39     }
     40 }
     41 
     42 // A class that throws BadErrorNoStringInit during static initialization.
     43 class BadInitNoStringInit {
     44     static int dummy;
     45     static {
     46         System.out.println("Static BadInitNoStringInit");
     47         if (true) {
     48             throw new BadErrorNoStringInit();
     49         }
     50     }
     51 }
     52 
     53 /**
     54  * Exceptions across method calls
     55  */
     56 public class Main {
     57     public static void exceptions_007() {
     58         try {
     59             catchAndRethrow();
     60         } catch (NullPointerException npe) {
     61             System.out.print("Got an NPE: ");
     62             System.out.println(npe.getMessage());
     63             npe.printStackTrace(System.out);
     64         }
     65     }
     66     public static void main (String args[]) {
     67         exceptions_007();
     68         exceptionsRethrowClassInitFailure();
     69         exceptionsRethrowClassInitFailureNoStringInit();
     70     }
     71 
     72     private static void catchAndRethrow() {
     73         try {
     74             throwNullPointerException();
     75         } catch (NullPointerException npe) {
     76             NullPointerException npe2;
     77             npe2 = new NullPointerException("second throw");
     78             npe2.initCause(npe);
     79             throw npe2;
     80         }
     81     }
     82 
     83     private static void throwNullPointerException() {
     84         throw new NullPointerException("first throw");
     85     }
     86 
     87     private static void exceptionsRethrowClassInitFailure() {
     88         try {
     89             try {
     90                 BadInit.dummy = 1;
     91                 throw new IllegalStateException("Should not reach here.");
     92             } catch (BadError e) {
     93                 System.out.println(e);
     94             }
     95 
     96             // Check if it works a second time.
     97 
     98             try {
     99                 BadInit.dummy = 1;
    100                 throw new IllegalStateException("Should not reach here.");
    101             } catch (NoClassDefFoundError e) {
    102                 System.out.println(e);
    103                 System.out.println(e.getCause());
    104             }
    105         } catch (Exception error) {
    106             error.printStackTrace(System.out);
    107         }
    108     }
    109 
    110     private static void exceptionsRethrowClassInitFailureNoStringInit() {
    111         try {
    112             try {
    113                 BadInitNoStringInit.dummy = 1;
    114                 throw new IllegalStateException("Should not reach here.");
    115             } catch (BadErrorNoStringInit e) {
    116                 System.out.println(e);
    117             }
    118 
    119             // Check if it works a second time.
    120 
    121             try {
    122                 BadInitNoStringInit.dummy = 1;
    123                 throw new IllegalStateException("Should not reach here.");
    124             } catch (NoClassDefFoundError e) {
    125                 System.out.println(e);
    126                 System.out.println(e.getCause());
    127             }
    128         } catch (Exception error) {
    129             error.printStackTrace(System.out);
    130         }
    131     }
    132 }
    133