Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4921296

ClassCastException needs more verbose detailMessage

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 6
    • hotspot
    • mustang
    • x86
    • linux, windows_2000

      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

            kamg Keith Mcguigan (Inactive)
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: