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/expected.txt b/test/008-exceptions/expected.txt
index ef6eaff..92c79dc 100644
--- a/test/008-exceptions/expected.txt
+++ b/test/008-exceptions/expected.txt
@@ -1,9 +1,12 @@
Got an NPE: second throw
java.lang.NullPointerException: second throw
- at Main.catchAndRethrow(Main.java:39)
- at Main.exceptions_007(Main.java:23)
- at Main.main(Main.java:31)
+ at Main.catchAndRethrow(Main.java:58)
+ at Main.exceptions_007(Main.java:41)
+ at Main.main(Main.java:49)
Caused by: java.lang.NullPointerException: first throw
- at Main.throwNullPointerException(Main.java:46)
- at Main.catchAndRethrow(Main.java:36)
+ at Main.throwNullPointerException(Main.java:65)
+ at Main.catchAndRethrow(Main.java:55)
... 2 more
+Static Init
+BadError: This is bad by convention
+BadError: This is bad by convention
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();
+ }
+ }
}