Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2008 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 /**
     18  * Test exception throwing.
     19  */
     20 public class Throw {
     21     public void throwNullPointerException() {
     22         throw new NullPointerException("npe!");
     23     }
     24 
     25     public void throwArithmeticException() {
     26         throw new ArithmeticException();
     27     }
     28 
     29     public void one() {
     30         System.out.println("Throw.one");
     31         try {
     32             throwNullPointerException();
     33             assert(false);
     34         } catch (Exception ex) {
     35             // good
     36             return;
     37         }
     38 
     39         assert(false);
     40     }
     41 
     42     public void twoA() {
     43         System.out.println("Throw.twoA");
     44         boolean gotN = false;
     45         boolean gotA = false;
     46         boolean gotWeird = false;
     47 
     48         try {
     49             try {
     50                 throwArithmeticException();
     51                 gotWeird = true;
     52             } catch (ArithmeticException ae) {
     53                 gotA = true;
     54             }
     55         } catch (NullPointerException npe) {
     56             gotN = true;
     57         }
     58 
     59         assert(gotA);
     60         assert(!gotN);
     61         assert(!gotWeird);
     62     }
     63 
     64     public void twoN() {
     65         System.out.println("Throw.twoN");
     66         boolean gotN = false;
     67         boolean gotA = false;
     68         boolean gotWeird = false;
     69 
     70         try {
     71             try {
     72                 throwNullPointerException();
     73                 gotWeird = true;
     74             } catch (ArithmeticException ae) {
     75                 gotA = true;
     76             }
     77         } catch (NullPointerException npe) {
     78             gotN = true;
     79         }
     80 
     81         assert(!gotA);
     82         assert(gotN);
     83         assert(!gotWeird);
     84     }
     85 
     86     public void rethrow() {
     87         System.out.println("Throw.rethrow");
     88         boolean caught = false;
     89         boolean lly = false;
     90         boolean second = false;
     91 
     92         try {
     93             try {
     94                 throwNullPointerException();
     95                 assert(false);
     96             } catch (Exception ex) {
     97                 if (ex instanceof ArithmeticException) {
     98                     assert(false);
     99                 }
    100                 if (ex instanceof NullPointerException) {
    101                     caught = true;
    102                     throw (NullPointerException) ex;
    103                 }
    104             } finally {
    105                 lly = true;
    106             }
    107         } catch (Exception ex) {
    108             second = true;
    109         }
    110 
    111         assert(caught);
    112         assert(lly);
    113         assert(second);
    114     }
    115 
    116     public static void run() {
    117         Throw th = new Throw();
    118 
    119         th.one();
    120         th.twoA();
    121         th.twoN();
    122         th.rethrow();
    123     }
    124 }
    125