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
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
- duplicates
-
JDK-4488551 RFE: More explicit error msg on ClassCastException
-
- Closed
-
- relates to
-
JDK-4826718 REGRESSION: ClassCastException no longer reveals the runtime type
-
- Resolved
-