diff options
Diffstat (limited to 'test/008-exceptions/src')
| -rw-r--r-- | test/008-exceptions/src/Main.java | 59 |
1 files changed, 51 insertions, 8 deletions
diff --git a/test/008-exceptions/src/Main.java b/test/008-exceptions/src/Main.java index 7f6d0c5956..b8231f12bd 100644 --- a/test/008-exceptions/src/Main.java +++ b/test/008-exceptions/src/Main.java @@ -14,20 +14,38 @@ * limitations under the License. */ -// An exception that doesn't have a <init>(String) method. +// An error class. class BadError extends Error { - public BadError() { - super("This is bad by convention"); + public BadError(String s) { + super("This is bad by convention: " + s); } } -// A class that throws BadException during static initialization. +// A class that throws BadError during static initialization. class BadInit { static int dummy; static { System.out.println("Static Init"); if (true) { - throw new BadError(); + throw new BadError("BadInit"); + } + } +} + +// An error that doesn't have a <init>(String) method. +class BadErrorNoStringInit extends Error { + public BadErrorNoStringInit() { + super("This is bad by convention"); + } +} + +// A class that throws BadErrorNoStringInit during static initialization. +class BadInitNoStringInit { + static int dummy; + static { + System.out.println("Static BadInitNoStringInit"); + if (true) { + throw new BadErrorNoStringInit(); } } } @@ -42,12 +60,13 @@ public class Main { } catch (NullPointerException npe) { System.out.print("Got an NPE: "); System.out.println(npe.getMessage()); - npe.printStackTrace(); + npe.printStackTrace(System.out); } } public static void main (String args[]) { exceptions_007(); exceptionsRethrowClassInitFailure(); + exceptionsRethrowClassInitFailureNoStringInit(); } private static void catchAndRethrow() { @@ -79,11 +98,35 @@ public class Main { try { BadInit.dummy = 1; throw new IllegalStateException("Should not reach here."); - } catch (BadError e) { + } catch (NoClassDefFoundError e) { + System.out.println(e); + System.out.println(e.getCause()); + } + } catch (Exception error) { + error.printStackTrace(System.out); + } + } + + private static void exceptionsRethrowClassInitFailureNoStringInit() { + try { + try { + BadInitNoStringInit.dummy = 1; + throw new IllegalStateException("Should not reach here."); + } catch (BadErrorNoStringInit e) { + System.out.println(e); + } + + // Check if it works a second time. + + try { + BadInitNoStringInit.dummy = 1; + throw new IllegalStateException("Should not reach here."); + } catch (NoClassDefFoundError e) { System.out.println(e); + System.out.println(e.getCause()); } } catch (Exception error) { - error.printStackTrace(); + error.printStackTrace(System.out); } } } |