Skip to main content

ClassCastException should always report incompatible type

3 replies [Last post]
patrikbeno
Offline
Joined: 2004-10-11
Points: 0

Default constructor should be deprecated and removed because it is useless: it does not provide information to resolve error.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mgrev
Offline
Joined: 2003-08-12
Points: 0

+1

patrikbeno
Offline
Joined: 2004-10-11
Points: 0

Further, everytime a CCE is thrown, message like this should be provided:

java.lang.ClassCastException: whatever.pckg.OffendingClass incompatible with expected another.pckg.ExpectedClass

Reason is that if message contains only offending class, it may not be sufficient information to resolve such error if it occurs in third party library without any sources or debug info (line numbers at least). You just don't know what was expected...

This should not be so hard :-) In fact, taking this to extreme, CCE should have single constructor only:

public ClassCastException(Class expected, Class offending) {} // plus optionally variant with additional explanatory message

but this is just a hardcore solution :-)

Message was edited by: patrikbeno

cowwoc
Offline
Joined: 2003-08-24
Points: 0

+1

Sounds like a simple fix and I agree that ClassCastException with no description is difficult to fix.