Class TypeCastException

All Implemented Interfaces:
Serializable

public class TypeCastException
extends ClassCastException
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()
    • assembleExplicitMessageAddon

      protected String assembleExplicitMessageAddon()
    • 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.