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 SummaryConstructors Constructor Description 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.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.
- 
Method SummaryModifier and Type Method Description StringassembleDetailString()StringassembleOutputString()StringgetMessage()Returns an assembled output String due to bad method design inThrowable.Stringmessage()Sadly, the Throwable implementation uses #getMessage() directly to print the exception.Objectsubject()The subject that could not be casted.Class<?>type()The target type the subject could not be casted to.Methods inherited from class java.lang.ThrowableaddSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
- 
Constructor Details- 
TypeCastExceptionInstantiates 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.
 
- 
TypeCastExceptionInstantiates 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- 
typeThe target type the subject could not be casted to.- Returns:
- the target type.
 
- 
subjectThe subject that could not be casted.- Returns:
- the subject.
 
- 
messageSadly, 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
- 
assembleOutputString
- 
getMessageReturns an assembled output String due to bad method design inThrowable. For the actual message getter, seemessage().- Overrides:
- getMessagein class- Throwable
- Returns:
- this exception type's generic string plus an explicit message if present.
 
 
-