United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4921296 : ClassCastException needs more verbose detailMessage

Details
Type:
Enhancement
Submit Date:
2003-09-11
Status:
Closed
Updated Date:
2012-10-13
Project Name:
JDK
Resolved Date:
2004-10-07
Component:
hotspot
OS:
linux,windows_2000
Sub-Component:
runtime
CPU:
x86
Priority:
P4
Resolution:
Fixed
Affected Versions:
6
Fixed Versions:

Related Reports
Duplicate:
Relates:

Sub Tasks

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



Hardware and Software, Engineered to Work Together