blob: 0ba5f801b5967a3f60be48c6997f26b8e60421e3 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
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
Andreas Gampe99babb62015-11-02 16:20:00 -080017// An error class.
Andreas Gampebfdcdc12015-04-22 18:10:36 -070018class BadError extends Error {
Andreas Gampe99babb62015-11-02 16:20:00 -080019 public BadError(String s) {
20 super("This is bad by convention: " + s);
Andreas Gampebfdcdc12015-04-22 18:10:36 -070021 }
22}
23
Andreas Gampe99babb62015-11-02 16:20:00 -080024// A class that throws BadError during static initialization.
Andreas Gampebfdcdc12015-04-22 18:10:36 -070025class BadInit {
Orion Hodson2731eb42020-07-24 12:10:12 +010026 static int intField;
Andreas Gampebfdcdc12015-04-22 18:10:36 -070027 static {
28 System.out.println("Static Init");
29 if (true) {
Andreas Gampe99babb62015-11-02 16:20:00 -080030 throw new BadError("BadInit");
31 }
32 }
33}
34
35// An error that doesn't have a <init>(String) method.
36class 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.
43class BadInitNoStringInit {
Orion Hodson2731eb42020-07-24 12:10:12 +010044 static int intField;
Andreas Gampe99babb62015-11-02 16:20:00 -080045 static {
46 System.out.println("Static BadInitNoStringInit");
47 if (true) {
48 throw new BadErrorNoStringInit();
Andreas Gampebfdcdc12015-04-22 18:10:36 -070049 }
50 }
51}
52
Vladimir Marko72ab6842017-01-20 19:32:50 +000053// A class that throws BadError during static initialization, serving as a super class.
54class BadSuperClass {
Orion Hodson2731eb42020-07-24 12:10:12 +010055 static int intField;
Vladimir Marko72ab6842017-01-20 19:32:50 +000056 static {
57 System.out.println("BadSuperClass Static Init");
58 if (true) {
59 throw new BadError("BadInit");
60 }
61 }
62}
63
64// A class that derives from BadSuperClass.
65class DerivedFromBadSuperClass extends BadSuperClass {
66}
67
jeffhao5d1ac922011-09-29 17:41:15 -070068/**
69 * Exceptions across method calls
70 */
71public class Main {
buzbee32412962012-06-26 16:27:56 -070072 public static void exceptions_007() {
jeffhao5d1ac922011-09-29 17:41:15 -070073 try {
74 catchAndRethrow();
75 } catch (NullPointerException npe) {
76 System.out.print("Got an NPE: ");
77 System.out.println(npe.getMessage());
Kevin Brodsky64daedd2015-12-14 10:15:04 +000078 npe.printStackTrace(System.out);
jeffhao5d1ac922011-09-29 17:41:15 -070079 }
80 }
Vladimir Marko72ab6842017-01-20 19:32:50 +000081 public static void main(String args[]) {
buzbee32412962012-06-26 16:27:56 -070082 exceptions_007();
Andreas Gampebfdcdc12015-04-22 18:10:36 -070083 exceptionsRethrowClassInitFailure();
Andreas Gampe99babb62015-11-02 16:20:00 -080084 exceptionsRethrowClassInitFailureNoStringInit();
Vladimir Marko72ab6842017-01-20 19:32:50 +000085 exceptionsForSuperClassInitFailure();
86 exceptionsInMultiDex();
buzbee32412962012-06-26 16:27:56 -070087 }
jeffhao5d1ac922011-09-29 17:41:15 -070088
89 private static void catchAndRethrow() {
90 try {
91 throwNullPointerException();
92 } catch (NullPointerException npe) {
93 NullPointerException npe2;
94 npe2 = new NullPointerException("second throw");
95 npe2.initCause(npe);
96 throw npe2;
97 }
98 }
99
100 private static void throwNullPointerException() {
101 throw new NullPointerException("first throw");
102 }
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700103
104 private static void exceptionsRethrowClassInitFailure() {
105 try {
106 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100107 BadInit.intField = 1;
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700108 throw new IllegalStateException("Should not reach here.");
109 } catch (BadError e) {
110 System.out.println(e);
111 }
112
113 // Check if it works a second time.
114
115 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100116 BadInit.intField = 1;
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700117 throw new IllegalStateException("Should not reach here.");
Andreas Gampecb086952015-11-02 16:20:00 -0800118 } catch (NoClassDefFoundError e) {
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700119 System.out.println(e);
Andreas Gampecb086952015-11-02 16:20:00 -0800120 System.out.println(e.getCause());
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700121 }
122 } catch (Exception error) {
Kevin Brodsky64daedd2015-12-14 10:15:04 +0000123 error.printStackTrace(System.out);
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700124 }
125 }
Andreas Gampe99babb62015-11-02 16:20:00 -0800126
127 private static void exceptionsRethrowClassInitFailureNoStringInit() {
128 try {
129 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100130 BadInitNoStringInit.intField = 1;
Andreas Gampe99babb62015-11-02 16:20:00 -0800131 throw new IllegalStateException("Should not reach here.");
132 } catch (BadErrorNoStringInit e) {
133 System.out.println(e);
134 }
135
136 // Check if it works a second time.
137
138 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100139 BadInitNoStringInit.intField = 1;
Andreas Gampe99babb62015-11-02 16:20:00 -0800140 throw new IllegalStateException("Should not reach here.");
Andreas Gampecb086952015-11-02 16:20:00 -0800141 } catch (NoClassDefFoundError e) {
Andreas Gampe99babb62015-11-02 16:20:00 -0800142 System.out.println(e);
Andreas Gampecb086952015-11-02 16:20:00 -0800143 System.out.println(e.getCause());
Andreas Gampe99babb62015-11-02 16:20:00 -0800144 }
145 } catch (Exception error) {
Kevin Brodsky64daedd2015-12-14 10:15:04 +0000146 error.printStackTrace(System.out);
Andreas Gampe99babb62015-11-02 16:20:00 -0800147 }
148 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000149
150 private static void exceptionsForSuperClassInitFailure() {
151 try {
152 // Resolve DerivedFromBadSuperClass.
Orion Hodson2731eb42020-07-24 12:10:12 +0100153 BadSuperClass.intField = 1;
Vladimir Marko72ab6842017-01-20 19:32:50 +0000154 throw new IllegalStateException("Should not reach here.");
155 } catch (BadError e) {
156 System.out.println(e);
157 } catch (Throwable t) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000158 t.printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000159 }
160 try {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000161 // Before splitting ClassStatus::kError into
162 // ClassStatus::kErrorUnresolved and ClassStatus::kErrorResolved,
Vladimir Marko72ab6842017-01-20 19:32:50 +0000163 // this would trigger a
164 // CHECK(super_class->IsResolved())
165 // failure in
166 // ClassLinker::LoadSuperAndInterfaces().
167 // After the change we're getting either VerifyError
168 // (for Optimizing) or NoClassDefFoundError wrapping
169 // BadError (for interpreter or JIT).
170 new DerivedFromBadSuperClass();
171 throw new IllegalStateException("Should not reach here.");
172 } catch (NoClassDefFoundError ncdfe) {
173 if (!(ncdfe.getCause() instanceof BadError)) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000174 ncdfe.getCause().printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000175 }
176 } catch (VerifyError e) {
177 } catch (Throwable t) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000178 t.printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000179 }
180 }
181
182 private static void exceptionsInMultiDex() {
183 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100184 MultiDexBadInit.intField = 1;
Vladimir Marko72ab6842017-01-20 19:32:50 +0000185 throw new IllegalStateException("Should not reach here.");
186 } catch (Error e) {
187 System.out.println(e);
188 } catch (Throwable t) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000189 t.printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000190 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000191 // Before splitting ClassStatus::kError into
192 // ClassStatus::kErrorUnresolved and ClassStatus::kErrorResolved,
Vladimir Marko72ab6842017-01-20 19:32:50 +0000193 // the exception from wrapper 1 would have been
194 // wrapped in NoClassDefFoundError but the exception
195 // from wrapper 2 would have been unwrapped.
196 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100197 MultiDexBadInitWrapper1.setIntField(1);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000198 throw new IllegalStateException("Should not reach here.");
199 } catch (NoClassDefFoundError ncdfe) {
200 System.out.println(ncdfe);
201 System.out.println(" cause: " + ncdfe.getCause());
202 } catch (Throwable t) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000203 t.printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000204 }
205 try {
Orion Hodson2731eb42020-07-24 12:10:12 +0100206 MultiDexBadInitWrapper2.setIntField(1);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000207 throw new IllegalStateException("Should not reach here.");
208 } catch (NoClassDefFoundError ncdfe) {
209 System.out.println(ncdfe);
210 System.out.println(" cause: " + ncdfe.getCause());
211 } catch (Throwable t) {
Kevin Brodskyf6c66c32015-12-17 14:13:00 +0000212 t.printStackTrace(System.out);
Vladimir Marko72ab6842017-01-20 19:32:50 +0000213 }
214 }
jeffhao5d1ac922011-09-29 17:41:15 -0700215}