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 Summary
Constructors 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 Summary
Modifier and Type Method Description String
assembleDetailString()
String
assembleOutputString()
String
getMessage()
Returns an assembled output String due to bad method design inThrowable
.String
message()
Sadly, the Throwable implementation uses #getMessage() directly to print the exception.Object
subject()
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.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
TypeCastException
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
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
The target type the subject could not be casted to.- Returns:
- the target type.
-
subject
The subject that could not be casted.- Returns:
- the subject.
-
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
-
assembleOutputString
-
getMessage
Returns an assembled output String due to bad method design inThrowable
. For the actual message getter, seemessage()
.- Overrides:
getMessage
in classThrowable
- Returns:
- this exception type's generic string plus an explicit message if present.
-