jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 17 | // An error class. |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 18 | class BadError extends Error { |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 19 | public BadError(String s) { |
| 20 | super("This is bad by convention: " + s); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 21 | } |
| 22 | } |
| 23 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 24 | // A class that throws BadError during static initialization. |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 25 | class BadInit { |
| 26 | static int dummy; |
| 27 | static { |
| 28 | System.out.println("Static Init"); |
| 29 | if (true) { |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 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(); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Exceptions across method calls |
| 55 | */ |
| 56 | public class Main { |
buzbee | 3241296 | 2012-06-26 16:27:56 -0700 | [diff] [blame] | 57 | public static void exceptions_007() { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 58 | try { |
| 59 | catchAndRethrow(); |
| 60 | } catch (NullPointerException npe) { |
| 61 | System.out.print("Got an NPE: "); |
| 62 | System.out.println(npe.getMessage()); |
Kevin Brodsky | 64daedd | 2015-12-14 10:15:04 +0000 | [diff] [blame] | 63 | npe.printStackTrace(System.out); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
buzbee | 3241296 | 2012-06-26 16:27:56 -0700 | [diff] [blame] | 66 | public static void main (String args[]) { |
| 67 | exceptions_007(); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 68 | exceptionsRethrowClassInitFailure(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 69 | exceptionsRethrowClassInitFailureNoStringInit(); |
buzbee | 3241296 | 2012-06-26 16:27:56 -0700 | [diff] [blame] | 70 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 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 | } |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 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."); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 101 | } catch (NoClassDefFoundError e) { |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 102 | System.out.println(e); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 103 | System.out.println(e.getCause()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 104 | } |
| 105 | } catch (Exception error) { |
Kevin Brodsky | 64daedd | 2015-12-14 10:15:04 +0000 | [diff] [blame] | 106 | error.printStackTrace(System.out); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 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."); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 124 | } catch (NoClassDefFoundError e) { |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 125 | System.out.println(e); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 126 | System.out.println(e.getCause()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 127 | } |
| 128 | } catch (Exception error) { |
Kevin Brodsky | 64daedd | 2015-12-14 10:15:04 +0000 | [diff] [blame] | 129 | error.printStackTrace(System.out); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 130 | } |
| 131 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 132 | } |