JDK-4488551 : RFE: More explicit error msg on ClassCastException
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 1.3.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2001-08-06
  • Updated: 2004-05-21
  • Resolved: 2004-05-06
Related Reports
Duplicate :  

Name: yyT116575			Date: 08/06/2001

java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

When executing

>  Object obj = new Integer(0);
>  String s = (String) obj;

the current error message is as follows:

>  Exception in thread "main" java.lang.ClassCastException: java.lang.Integer
>        at T.main(T.java:13)

It would be better to have a more explicit error message like:

>  Exception in thread "main" java.lang.ClassCastException: cannot cast from
java.lang.Integer to java.lang.String
>        at T.main(T.java:13)

This is especially true in a distributed environment where some classes
(stubs/skeletons) are generated and not all source code is available.
(Review ID: 129472) 

EVALUATION public class test { public static void main(String[] arg) { Object obj = new Integer(0); String s = (String) obj; System.out.print(s.toString()); } } Testcase above. Tested in JDK 1.4.1 b11 Compile it and execute it, error will occur java test Exception in thread "main" java.lang.ClassCastException at test.main(test.java:7) ###@###.### 2002-05-17 For C2 and interpreter, it's actually getting worse in 1.4.1, due to changes in C2 build 20010927-160457 (final fix for 4476327). For C1, the message still contains the object type. To fix the problem, we need to make a special case when generating the ClassCastException to include more information in the exception. ###@###.### 2002-11-07