JDK-7031482 : ClassCastException should indicate actual and expected classes
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6u24,8
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_7
  • CPU: generic,x86
  • Submitted: 2011-03-27
  • Updated: 2012-09-29
  • Resolved: 2012-09-28
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
The detail message associated with ClassCastException should always indicate the actual and expected type of the object being casted.

JUSTIFICATION :
It is impossible to fix ClassCastExceptions without knowing the actual and expect types. Currently the only way to fix such bugs is to reproduce the issue in a debugger, something that is not always possible or easy to do.

Comments
I've had a similar request from Rich Berlin at Guidewire. In addition, it would be nice to include some information about the object that was cast, perhaps calling toString() on it, or even including the instance reference itself in the exception. Doing this may involve unacceptable leakage of information, however.
29-09-2012

EVALUATION Adding the types all the time may be too expensive. However, perhaps the exception type could at be modified to optionally allow the additional type information to be passed in.
23-07-2012