blob: b8231f12bd37e587742a03f59d086ceb61dea053 [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 {
26 static int dummy;
27 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 {
44 static int dummy;
45 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
jeffhao5d1ac922011-09-29 17:41:15 -070053/**
54 * Exceptions across method calls
55 */
56public class Main {
buzbee32412962012-06-26 16:27:56 -070057 public static void exceptions_007() {
jeffhao5d1ac922011-09-29 17:41:15 -070058 try {
59 catchAndRethrow();
60 } catch (NullPointerException npe) {
61 System.out.print("Got an NPE: ");
62 System.out.println(npe.getMessage());
Kevin Brodsky64daedd2015-12-14 10:15:04 +000063 npe.printStackTrace(System.out);
jeffhao5d1ac922011-09-29 17:41:15 -070064 }
65 }
buzbee32412962012-06-26 16:27:56 -070066 public static void main (String args[]) {
67 exceptions_007();
Andreas Gampebfdcdc12015-04-22 18:10:36 -070068 exceptionsRethrowClassInitFailure();
Andreas Gampe99babb62015-11-02 16:20:00 -080069 exceptionsRethrowClassInitFailureNoStringInit();
buzbee32412962012-06-26 16:27:56 -070070 }
jeffhao5d1ac922011-09-29 17:41:15 -070071
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 }
Andreas Gampebfdcdc12015-04-22 18:10:36 -070086
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.");
Andreas Gampecb086952015-11-02 16:20:00 -0800101 } catch (NoClassDefFoundError e) {
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700102 System.out.println(e);
Andreas Gampecb086952015-11-02 16:20:00 -0800103 System.out.println(e.getCause());
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700104 }
105 } catch (Exception error) {
Kevin Brodsky64daedd2015-12-14 10:15:04 +0000106 error.printStackTrace(System.out);
Andreas Gampebfdcdc12015-04-22 18:10:36 -0700107 }
108 }
Andreas Gampe99babb62015-11-02 16:20:00 -0800109
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.");
Andreas Gampecb086952015-11-02 16:20:00 -0800124 } catch (NoClassDefFoundError e) {
Andreas Gampe99babb62015-11-02 16:20:00 -0800125 System.out.println(e);
Andreas Gampecb086952015-11-02 16:20:00 -0800126 System.out.println(e.getCause());
Andreas Gampe99babb62015-11-02 16:20:00 -0800127 }
128 } catch (Exception error) {
Kevin Brodsky64daedd2015-12-14 10:15:04 +0000129 error.printStackTrace(System.out);
Andreas Gampe99babb62015-11-02 16:20:00 -0800130 }
131 }
jeffhao5d1ac922011-09-29 17:41:15 -0700132}