JDK-4836801 : More Throwables should support nested Throwables
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0,1.4.1
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic,linux,windows_2000
  • CPU: generic,x86
  • Submitted: 2003-03-24
  • Updated: 2017-05-16
  • Resolved: 2003-08-30
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.
Other
5.0 tigerFixed
Related Reports
Duplicate :  
Duplicate :  
Duplicate :  
Description

Name: gm110360			Date: 03/24/2003


A DESCRIPTION OF THE REQUEST :
More Throwables should support nested Throwables. The root Throwable class has some very nice API for this support, but subclasses rarely take advantage of it. Constructors that tack the cause in the argument encourage developers to use it, and write better code.

JUSTIFICATION :
I am currently catching a declared Exception from a library. This libary exception indicates an illegal state. I'd like to throw an IllegalStateException, but have to jump through some hoops because IllegalStateException's constructor does not take a nested Throwable.

EXPECTED VERSUS ACTUAL BEHAVIOR :
I'd like to use the code

    throw new IllegalStateException("bad state details",myException);
If I use that code, it won't compile.

---------- BEGIN SOURCE ----------
A test case containing

    throw new IllegalStateException("bad state details",new Exception("test"));

should reproduce the problem nicely.
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
I can use

    Throwable thrown = new IllegalStateException("message");
    thrown.initCause(cause);
    throw thrown;

but the shorter API will encourage others to nest exceptions much better.
(Review ID: 182957) 
======================================================================

Name: nt126004			Date: 05/15/2003


FULL PRODUCT VERSION :
java version "1.4.1_02"

A DESCRIPTION OF THE PROBLEM :
  To my surprise in JDK 1.4.1_02 some exceptions like
IllegalArgumentException, IllegalStateException
do not support constructors for exception chaining,
e.g. IllegalStateException(Throwable cause)

The method Throwable.initCause(Throwable)
however allows the setting of the cause.

The result is ugly code if I want to set the cause:
instead of
throw new new IllegalStateException(theCause);

I must write:
IllegalStateException ex = new IllegalStateException();
ex.initCause(theCause);
throw ex;


Or is this omission of constructors on purpose for some reason?
(Review ID: 185824)
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger FIXED IN: tiger INTEGRATED IN: tiger tiger-b18
14-06-2004

EVALUATION I tried to choose carefully when deciding which standard exceptions got chaining constructors, but I missed a few where it turns out that chaining would have been valuable. We will add these in a subsequent release. In the meantime, remember that you can always use the initCause method, which was provided for precisely this eventuality. ###@###.### 2003-05-21
21-05-2003