JDK-6935997 : Please add a nested throwable constructor to AssertionError
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 7
  • Priority: P5
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2010-03-18
  • Updated: 2022-03-04
  • Resolved: 2011-05-18
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b100Fixed
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
AssertionError does not have constructors that takes message and a nested throwable. Please add one.

public AssertionError(String message,Throwable cause) { ...}

JUSTIFICATION :
I frequently see code that catches a checked exception that should never be thrown. Sometimes it asserts false, sometimes it throws a RuntimeException, sometimes I'll see an empty catch block, maybe with a comment.

The right thing to do is to throw an AssertionError.

catch(ImpossibleException ie) {
    throw new AssertionError("Flow of control should never throw this exception because blah blah blah.",ie);
}

It's a pretty minor request; AssertionError does have an initCause() method that works fine. Developers rarely take that extra simple step to use initCause(). However, they do use the two-argument constructor in many other places. One was surprised not to find it when I pointed out initCause().

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I'd like to see a new constructor added to AssertionError. Other developers would use that without blinking.
ACTUAL -
The constructor isn't there, so I use initCause. Other developers are astonished when I suggest it.

CUSTOMER SUBMITTED WORKAROUND :
intiCause() works fine for me. Not for others.

Comments
SUGGESTED FIX # HG changeset patch # User darcy # Date 1276048337 25200 # Node ID a21e3a29ca9d0de67d8c754ae27dbccb0740f481 # Parent 489c1720757b3eb6f3c194c371871ab1ea66100f 6935997: Please add a nested throwable constructor to AssertionError Reviewed-by: martin, forax, wetmore --- a/src/share/classes/java/lang/AssertionError.java Tue Jun 08 10:46:14 2010 +0100 +++ b/src/share/classes/java/lang/AssertionError.java Tue Jun 08 18:52:17 2010 -0700 @@ -66,7 +66,7 @@ public class AssertionError extends Erro * defined in <i>The Java Language Specification, Second * Edition</i>, Section 15.18.1.1. *<p> - * If the specified object is an instance of <tt>Throwable</tt>, it + * If the specified object is an instance of {@code Throwable}, it * becomes the <i>cause</i> of the newly constructed assertion error. * * @param detailMessage value to be used in constructing detail message @@ -149,4 +149,21 @@ public class AssertionError extends Erro public AssertionError(double detailMessage) { this("" + detailMessage); } + + /** + * Constructs a new {@code AssertionError} with the specified + * detail message and cause. + * + * <p>Note that the detail message associated with + * {@code cause} is <i>not</i> automatically incorporated in + * this error's detail message. + * + * @param message the detail message, may be {@code null} + * @param cause the cause, may be {@code null} + * + * @since 1.7 + */ + public AssertionError(String message, Throwable cause) { + super(message, cause); + } } --- a/src/share/classes/java/security/Security.java Tue Jun 08 10:46:14 2010 +0100 +++ b/src/share/classes/java/security/Security.java Tue Jun 08 18:52:17 2010 -0700 @@ -678,7 +678,7 @@ public final class Security { spiMap.put(type, clazz); return clazz; } catch (ClassNotFoundException e) { - throw (Error)new AssertionError("Spi class not found").initCause(e); + throw new AssertionError("Spi class not found", e); } }
09-06-2010

PUBLIC COMMENTS See http://hg.openjdk.java.net/jdk7/tl/jdk/rev/a21e3a29ca9d
09-06-2010

EVALUATION A fine idea.
04-06-2010