8012044: Give more information about self-suppression from Throwable.addSuppressed
authordarcy
Thu, 25 Apr 2013 09:37:03 -0700
changeset 17176 d7ee1e315fe7
parent 17175 d9614db37fd2
child 17177 59a964c1d518
8012044: Give more information about self-suppression from Throwable.addSuppressed Reviewed-by: alanb, dholmes
jdk/src/share/classes/java/lang/Throwable.java
jdk/test/java/lang/Throwable/SuppressedExceptions.java
--- a/jdk/src/share/classes/java/lang/Throwable.java	Wed Apr 24 21:27:52 2013 +0000
+++ b/jdk/src/share/classes/java/lang/Throwable.java	Thu Apr 25 09:37:03 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -453,9 +453,10 @@
      */
     public synchronized Throwable initCause(Throwable cause) {
         if (this.cause != this)
-            throw new IllegalStateException("Can't overwrite cause");
+            throw new IllegalStateException("Can't overwrite cause with " +
+                                            Objects.toString(cause, "a null"), this);
         if (cause == this)
-            throw new IllegalArgumentException("Self-causation not permitted");
+            throw new IllegalArgumentException("Self-causation not permitted", this);
         this.cause = cause;
         return this;
     }
@@ -1039,7 +1040,7 @@
      */
     public final synchronized void addSuppressed(Throwable exception) {
         if (exception == this)
-            throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE);
+            throw new IllegalArgumentException(SELF_SUPPRESSION_MESSAGE, exception);
 
         if (exception == null)
             throw new NullPointerException(NULL_CAUSE_MESSAGE);
--- a/jdk/test/java/lang/Throwable/SuppressedExceptions.java	Wed Apr 24 21:27:52 2013 +0000
+++ b/jdk/test/java/lang/Throwable/SuppressedExceptions.java	Thu Apr 25 09:37:03 2013 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
 
 /*
  * @test
- * @bug     6911258 6962571 6963622 6991528 7005628
+ * @bug     6911258 6962571 6963622 6991528 7005628 8012044
  * @summary Basic tests of suppressed exceptions
  * @author  Joseph D. Darcy
  */
@@ -40,6 +40,7 @@
         serializationTest();
         selfReference();
         noModification();
+        initCausePlumbing();
     }
 
     private static void noSelfSuppression() {
@@ -48,7 +49,9 @@
             throwable.addSuppressed(throwable);
             throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");
         } catch (IllegalArgumentException iae) {
-            ; // Expected
+            // Expected to be here
+            if (iae.getCause() != throwable)
+                throw new RuntimeException("Bad cause after self-suppresion.");
         }
     }
 
@@ -208,4 +211,36 @@
             super("The medium.", null, enableSuppression, true);
         }
     }
+
+    private static void initCausePlumbing() {
+        Throwable t1 = new Throwable();
+        Throwable t2 = new Throwable("message", t1);
+        Throwable t3 = new Throwable();
+
+        try {
+            t2.initCause(t3);
+            throw new RuntimeException("Shouldn't reach.");
+        } catch (IllegalStateException ise) {
+            if (ise.getCause() != t2)
+                throw new RuntimeException("Unexpected cause in ISE", ise);
+            Throwable[] suppressed = ise.getSuppressed();
+            if (suppressed.length !=  0)
+                throw new RuntimeException("Bad suppression in ISE", ise);
+        }
+
+        try {
+            t2.initCause(null);
+            throw new RuntimeException("Shouldn't reach.");
+        } catch (IllegalStateException ise) {
+            ; // Expected; don't want an NPE.
+        }
+
+        try {
+            t3.initCause(t3);
+            throw new RuntimeException("Shouldn't reach.");
+        } catch (IllegalArgumentException iae) {
+            if (iae.getCause() != t3)
+                throw new RuntimeException("Unexpected cause in ISE", iae);
+        }
+    }
 }