JDK-4921296 : ClassCastException needs more verbose detailMessage
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux,windows_2000
  • CPU: x86
  • Submitted: 2003-09-11
  • Updated: 2012-10-13
  • Resolved: 2004-10-07
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 Availabitlity Release.

To download the current JDK release, click here.
JDK 6
6 mustangFixed
Related Reports
Duplicate :  
Relates :  
Description
Name: gm110360			Date: 09/11/2003


A DESCRIPTION OF THE REQUEST :
ClassCastExceptions do not inform developers as to what the cast conversion attempted was.

JUSTIFICATION :
a more verbose error message in ClassCastException would eliminate the need for developers to add special try-catch code whenever we are debugging these exceptions.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
    Foo foo = (Foo) "wrong";

java.lang.ClassCastException: java.lang.String cannot be cast to a org.bogus.Foo

ACTUAL -
java.lang.ClassCastException has no message.

---------- BEGIN SOURCE ----------
    String s;
    try {
        s = (String) new Object();
    } catch (ClassCastException e) {
        if(e.getMessage() == null);
            throw new RuntimeException("ClassCastException should be verbose", e);
    }


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
    Foo foo;
    try {
        foo = (Foo) obj;
    } catch (ClassCastException e) {
        throw new RuntimeException(obj.getClass() + " not a " + Foo.class, e);
    }
(Incident Review ID: 201870) 
======================================================================
###@###.### 10/4/04 15:37 GMT

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

PUBLIC COMMENTS ClassCastException's message now contains both the target class name and the object class name.
2004-06-10

EVALUATION This is likely a duplicate of bug 4488551. In jdk1.4.1 and jdk1.4.2, no detail message was provided. As of tiger b26, a detail message identical to that observed in 1.4 is now provided on solaris and linux (no testing done on windows): $ java -version java version "1.5.0-beta" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta-b26) Java HotSpot(TM) Client VM (build 1.5.0-beta-b26, mixed mode) $ java Test Exception in thread "main" java.lang.RuntimeException: ClassCastException should be verbose at Test.main(Test.java:8) Caused by: java.lang.ClassCastException: java.lang.Object at Test.main(Test.java:5) -- iag@sfbay 2003-10-29
2003-10-29