From bfdcdc1e2c0af34aeaf7b5b4d499975e0c3157be Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Wed, 22 Apr 2015 18:10:36 -0700 Subject: ART: Fix re-throwing failures of non-convention errors While it is convention that Throwable subclasses should have a constructor with a String argument, that is not rigorously enforced. So if a static initializer throws an error that omits that constructor, we must not provide a message when trying to throw again. Bug: 20495321 Bug: 20497840 Change-Id: Ia4334fa24223750f90a8f2732f1eb1e738575e8d --- test/008-exceptions/src/Main.java | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'test/008-exceptions/src/Main.java') diff --git a/test/008-exceptions/src/Main.java b/test/008-exceptions/src/Main.java index 1f76f12460..7f6d0c5956 100644 --- a/test/008-exceptions/src/Main.java +++ b/test/008-exceptions/src/Main.java @@ -14,6 +14,24 @@ * limitations under the License. */ +// An exception that doesn't have a (String) method. +class BadError extends Error { + public BadError() { + super("This is bad by convention"); + } +} + +// A class that throws BadException during static initialization. +class BadInit { + static int dummy; + static { + System.out.println("Static Init"); + if (true) { + throw new BadError(); + } + } +} + /** * Exceptions across method calls */ @@ -29,6 +47,7 @@ public class Main { } public static void main (String args[]) { exceptions_007(); + exceptionsRethrowClassInitFailure(); } private static void catchAndRethrow() { @@ -45,4 +64,26 @@ public class Main { private static void throwNullPointerException() { throw new NullPointerException("first throw"); } + + private static void exceptionsRethrowClassInitFailure() { + try { + try { + BadInit.dummy = 1; + throw new IllegalStateException("Should not reach here."); + } catch (BadError e) { + System.out.println(e); + } + + // Check if it works a second time. + + try { + BadInit.dummy = 1; + throw new IllegalStateException("Should not reach here."); + } catch (BadError e) { + System.out.println(e); + } + } catch (Exception error) { + error.printStackTrace(); + } + } } -- cgit v1.2.3-59-g8ed1b