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
diff --git a/test/008-exceptions/src/Main.java b/test/008-exceptions/src/Main.java
index 1f76f12..7f6d0c5 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 <init>(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 static void main (String args[]) {
exceptions_007();
+ exceptionsRethrowClassInitFailure();
}
private static void catchAndRethrow() {
@@ -45,4 +64,26 @@
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();
+ }
+ }
}