United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4836801 More Throwables should support nested Throwables
JDK-4836801 : More Throwables should support nested Throwables

Details
Type:
Enhancement
Submit Date:
2003-03-24
Status:
Resolved
Updated Date:
2003-08-30
Project Name:
JDK
Resolved Date:
2003-08-30
Component:
core-libs
OS:
linux,generic,windows_2000
Sub-Component:
java.lang
CPU:
x86,generic
Priority:
P4
Resolution:
Fixed
Affected Versions:
1.4.0,1.4.1
Fixed Versions:
5.0 (tiger)

Related Reports
Duplicate:
Duplicate:
Duplicate:

Sub Tasks

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
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
                                     
2003-05-21
CONVERTED DATA

BugTraq+ Release Management Values

COMMIT TO FIX:
tiger

FIXED IN:
tiger

INTEGRATED IN:
tiger
tiger-b18


                                     
2004-06-14



Hardware and Software, Engineered to Work Together