xref: /aosp_15_r20/art/test/003-omnibus-opcodes/src/Throw.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2008 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker /**
18*795d594fSAndroid Build Coastguard Worker  * Test exception throwing.
19*795d594fSAndroid Build Coastguard Worker  */
20*795d594fSAndroid Build Coastguard Worker public class Throw {
throwNullPointerException()21*795d594fSAndroid Build Coastguard Worker     public void throwNullPointerException() {
22*795d594fSAndroid Build Coastguard Worker         throw new NullPointerException("npe!");
23*795d594fSAndroid Build Coastguard Worker     }
24*795d594fSAndroid Build Coastguard Worker 
throwArithmeticException()25*795d594fSAndroid Build Coastguard Worker     public void throwArithmeticException() {
26*795d594fSAndroid Build Coastguard Worker         throw new ArithmeticException();
27*795d594fSAndroid Build Coastguard Worker     }
28*795d594fSAndroid Build Coastguard Worker 
one()29*795d594fSAndroid Build Coastguard Worker     public void one() {
30*795d594fSAndroid Build Coastguard Worker         System.out.println("Throw.one");
31*795d594fSAndroid Build Coastguard Worker         try {
32*795d594fSAndroid Build Coastguard Worker             throwNullPointerException();
33*795d594fSAndroid Build Coastguard Worker             Main.assertTrue(false);
34*795d594fSAndroid Build Coastguard Worker         } catch (Exception ex) {
35*795d594fSAndroid Build Coastguard Worker             // good
36*795d594fSAndroid Build Coastguard Worker             return;
37*795d594fSAndroid Build Coastguard Worker         }
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(false);
40*795d594fSAndroid Build Coastguard Worker     }
41*795d594fSAndroid Build Coastguard Worker 
twoA()42*795d594fSAndroid Build Coastguard Worker     public void twoA() {
43*795d594fSAndroid Build Coastguard Worker         System.out.println("Throw.twoA");
44*795d594fSAndroid Build Coastguard Worker         boolean gotN = false;
45*795d594fSAndroid Build Coastguard Worker         boolean gotA = false;
46*795d594fSAndroid Build Coastguard Worker         boolean gotWeird = false;
47*795d594fSAndroid Build Coastguard Worker 
48*795d594fSAndroid Build Coastguard Worker         try {
49*795d594fSAndroid Build Coastguard Worker             try {
50*795d594fSAndroid Build Coastguard Worker                 throwArithmeticException();
51*795d594fSAndroid Build Coastguard Worker                 gotWeird = true;
52*795d594fSAndroid Build Coastguard Worker             } catch (ArithmeticException ae) {
53*795d594fSAndroid Build Coastguard Worker                 gotA = true;
54*795d594fSAndroid Build Coastguard Worker             }
55*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
56*795d594fSAndroid Build Coastguard Worker             gotN = true;
57*795d594fSAndroid Build Coastguard Worker         }
58*795d594fSAndroid Build Coastguard Worker 
59*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(gotA);
60*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(!gotN);
61*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(!gotWeird);
62*795d594fSAndroid Build Coastguard Worker     }
63*795d594fSAndroid Build Coastguard Worker 
twoN()64*795d594fSAndroid Build Coastguard Worker     public void twoN() {
65*795d594fSAndroid Build Coastguard Worker         System.out.println("Throw.twoN");
66*795d594fSAndroid Build Coastguard Worker         boolean gotN = false;
67*795d594fSAndroid Build Coastguard Worker         boolean gotA = false;
68*795d594fSAndroid Build Coastguard Worker         boolean gotWeird = false;
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker         try {
71*795d594fSAndroid Build Coastguard Worker             try {
72*795d594fSAndroid Build Coastguard Worker                 throwNullPointerException();
73*795d594fSAndroid Build Coastguard Worker                 gotWeird = true;
74*795d594fSAndroid Build Coastguard Worker             } catch (ArithmeticException ae) {
75*795d594fSAndroid Build Coastguard Worker                 gotA = true;
76*795d594fSAndroid Build Coastguard Worker             }
77*795d594fSAndroid Build Coastguard Worker         } catch (NullPointerException npe) {
78*795d594fSAndroid Build Coastguard Worker             gotN = true;
79*795d594fSAndroid Build Coastguard Worker         }
80*795d594fSAndroid Build Coastguard Worker 
81*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(!gotA);
82*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(gotN);
83*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(!gotWeird);
84*795d594fSAndroid Build Coastguard Worker     }
85*795d594fSAndroid Build Coastguard Worker 
rethrow()86*795d594fSAndroid Build Coastguard Worker     public void rethrow() {
87*795d594fSAndroid Build Coastguard Worker         System.out.println("Throw.rethrow");
88*795d594fSAndroid Build Coastguard Worker         boolean caught = false;
89*795d594fSAndroid Build Coastguard Worker         boolean lly = false;
90*795d594fSAndroid Build Coastguard Worker         boolean second = false;
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker         try {
93*795d594fSAndroid Build Coastguard Worker             try {
94*795d594fSAndroid Build Coastguard Worker                 throwNullPointerException();
95*795d594fSAndroid Build Coastguard Worker                 Main.assertTrue(false);
96*795d594fSAndroid Build Coastguard Worker             } catch (Exception ex) {
97*795d594fSAndroid Build Coastguard Worker                 if (ex instanceof ArithmeticException) {
98*795d594fSAndroid Build Coastguard Worker                     Main.assertTrue(false);
99*795d594fSAndroid Build Coastguard Worker                 }
100*795d594fSAndroid Build Coastguard Worker                 if (ex instanceof NullPointerException) {
101*795d594fSAndroid Build Coastguard Worker                     caught = true;
102*795d594fSAndroid Build Coastguard Worker                     throw (NullPointerException) ex;
103*795d594fSAndroid Build Coastguard Worker                 }
104*795d594fSAndroid Build Coastguard Worker             } finally {
105*795d594fSAndroid Build Coastguard Worker                 lly = true;
106*795d594fSAndroid Build Coastguard Worker             }
107*795d594fSAndroid Build Coastguard Worker         } catch (Exception ex) {
108*795d594fSAndroid Build Coastguard Worker             second = true;
109*795d594fSAndroid Build Coastguard Worker         }
110*795d594fSAndroid Build Coastguard Worker 
111*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(caught);
112*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(lly);
113*795d594fSAndroid Build Coastguard Worker         Main.assertTrue(second);
114*795d594fSAndroid Build Coastguard Worker     }
115*795d594fSAndroid Build Coastguard Worker 
run()116*795d594fSAndroid Build Coastguard Worker     public static void run() {
117*795d594fSAndroid Build Coastguard Worker         Throw th = new Throw();
118*795d594fSAndroid Build Coastguard Worker 
119*795d594fSAndroid Build Coastguard Worker         th.one();
120*795d594fSAndroid Build Coastguard Worker         th.twoA();
121*795d594fSAndroid Build Coastguard Worker         th.twoN();
122*795d594fSAndroid Build Coastguard Worker         th.rethrow();
123*795d594fSAndroid Build Coastguard Worker     }
124*795d594fSAndroid Build Coastguard Worker }
125