Class TypeCastException

All Implemented Interfaces:
Serializable

public class TypeCastException
extends ClassCastException
ClassCastException implemented properly.

First of all: The correct term is "TypeCastException" and not "ClassCastException" because it not only handles classes but also interfaces (and the general term for both of them is "type"). Interfaces may be hackily defined as "class" instead of "type" on the bytecode level (god knows why), but that does not change the proper term on the level of design / API / source code.

Second: Proper exception design is to reference the involved objects if possible to keep them programmatically processible. Cramming everything just into contextless and runtime expensive plain strings is so noobish I can't tell. Exceptions are control flow vehicles, not just debug messages for the developer. Designing them only as the latter proves very dilettantic understanding of Java or modern OOP in general.

One might think that the original Java developers themselves would get basic stuff like that right back then. Sadly, they apparently didn't.

See Also:
Serialized Form
  • Constructor Details

    • TypeCastException

      public TypeCastException​(Class<?> type, Object subject)
      Instantiates a new instance with the target type and the subject instance that caused the exception and no explicit message.
      Parameters:
      type - the target type the subject could not be casted to.
      subject - the subject that could not be casted.
    • TypeCastException

      public TypeCastException​(Class<?> type, Object subject, String message)
      Instantiates a new instance with the target type and the subject instance that caused the exception and an explicit message.
      Parameters:
      type - the target type the subject could not be casted to.
      subject - the subject that could not be casted.
      message - an arbitrary string used as a custom message.
  • Method Details

    • type

      public final Class<?> type()
      The target type the subject could not be casted to.
      Returns:
      the target type.
    • subject

      public final Object subject()
      The subject that could not be casted.
      Returns:
      the subject.
    • message

      public final String message()
      Sadly, the Throwable implementation uses #getMessage() directly to print the exception. This is a concern conflict: getMessage should actually be the getter for the explicit message. But it is used as the String representating method as well. So a output message generically assembling the output string must override the getter. As this hides the actual getting functionality, a workaround accessor method has to be provided for potential subclasses.
      Returns:
      the explicit message string passed to the constructor when creating this instance.
    • assembleDetailString

      public String assembleDetailString()
    • assembleOutputString

      public String assembleOutputString()
    • getMessage

      public String getMessage()
      Returns an assembled output String due to bad method design in Throwable. For the actual message getter, see message().
      Overrides:
      getMessage in class Throwable
      Returns:
      this exception type's generic string plus an explicit message if present.