Class TypeMismatchException

All Implemented Interfaces:
Serializable

@Beta public class TypeMismatchException extends RuntimeException
Exception thrown when a value cannot be cast to the expected type.

This exists mainly to provide more information in certain situations than ClassCastException does. For example, when checking that a List contains only Integer values, but a Double is found. Or when checking that a Map contains the expected key and value types.

See Also:
  • Method Details

    • expectedType

      public Class<?> expectedType()
      Gets the expected type of the value that caused the type mismatch.
      Returns:
      the Class object representing the expected type
    • actualType

      public Class<?> actualType()
      Gets the actual type of the value that caused the type mismatch.
      Returns:
      the Class object representing the actual type
    • forUnexpectedType

      public static TypeMismatchException forUnexpectedType(Class<?> expectedType, Class<?> actualType)
      Factory method to create a new instance with a standardized message for a type mismatch and no cause.
      Parameters:
      expectedType - the expected type of the value
      actualType - the actual type of the value
      Returns:
      a new instance with a descriptive message
    • forUnexpectedTypeWithCause

      public static TypeMismatchException forUnexpectedTypeWithCause(Class<?> expectedType, Class<?> actualType, ClassCastException cause)
      Factory method to create a new instance with a standardized message for a type mismatch.
      Parameters:
      expectedType - the expected type of the value
      actualType - the actual type of the value
      cause - the ClassCastException that occurred during the cast attempt
      Returns:
      a new instance with a descriptive message
    • forUnexpectedCollectionElementType

      public static TypeMismatchException forUnexpectedCollectionElementType(Class<?> collectionType, Class<?> expectedType, Class<?> actualType)
      Factory method to create a new instance with a standardized message for a type mismatch on a collection element. The returned exception has no cause.
      Parameters:
      collectionType - the type of collection, such as Collection, List, or Set
      expectedType - the expected type of the collection elements
      actualType - the actual type found in the collection
      Returns:
      a new instance with a descriptive message
    • forUnexpectedMapKeyType

      public static TypeMismatchException forUnexpectedMapKeyType(Class<?> expectedKeyType, Class<?> actualKeyType)
      Factory method to create a new instance with a standardized message for a map key type mismatch. The returned exception has no cause.
      Parameters:
      expectedKeyType - the expected type of keys in the map
      actualKeyType - the actual type of key found in the map
      Returns:
      a new instance with a descriptive message
    • forUnexpectedMapValueType

      public static TypeMismatchException forUnexpectedMapValueType(Class<?> expectedValueType, Class<?> actualValueType)
      Factory method to create a new instance with a standardized message for a map value type mismatch. The returned exception has no cause.
      Parameters:
      expectedValueType - the expected type of values in the map
      actualValueType - the actual type of value found in the map
      Returns:
      a new instance with a descriptive message
    • forUnexpectedTypeWithMessage

      public static TypeMismatchException forUnexpectedTypeWithMessage(String message, Class<?> expectedType, Class<?> actualType, ClassCastException cause)
      Factory method to create a new instance of TypeMismatchException with a custom message, expected type, actual type, and an optional cause.
      Parameters:
      message - a custom descriptive message explaining the type mismatch
      expectedType - the expected type of the value
      actualType - the actual type of the value
      cause - the (optional) ClassCastException that occurred during the cast attempt
      Returns:
      a new instance of TypeMismatchException representing the type mismatch