Class OpenMatchException

All Implemented Interfaces:
OpenExceptionMeta, Serializable

public class OpenMatchException extends OpenFunctionalException
OpenMatchException - Exception for pattern matching failures OpenMatchException - 模式匹配失败的异常

Thrown when pattern matching fails to find a matching case for the input value.

当模式匹配无法为输入值找到匹配的分支时抛出。

Features | 主要功能:

  • Captures unmatched value - 捕获未匹配的值
  • Supports sealed type exhaustiveness errors - 支持密封类型完备性错误
  • Factory methods for common scenarios - 常见场景的工厂方法

Usage Examples | 使用示例:

// Thrown when no pattern matches
throw OpenMatchException.noMatch(value);

// Thrown for non-exhaustive sealed type matching
throw OpenMatchException.exhaustive(value, Shape.class);

// Access unmatched value
try {
    OpenMatch.of(value).orElseThrow();
} catch (OpenMatchException e) {
    Object unmatched = e.unmatchedValue();
}

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是 (不可变)
  • Serializable: Yes - 可序列化: 是
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • OpenMatchException

      public OpenMatchException(String message)
      Create exception with message 创建带消息的异常
      Parameters:
      message - error message | 错误消息
    • OpenMatchException

      public OpenMatchException(String message, Object unmatchedValue)
      Create exception with message and unmatched value 创建带消息和未匹配值的异常
      Parameters:
      message - error message | 错误消息
      unmatchedValue - the value that was not matched | 未匹配的值
    • OpenMatchException

      public OpenMatchException(String errorCode, String message, Object unmatchedValue)
      Create exception with error code, message and unmatched value 创建带错误码、消息和未匹配值的异常
      Parameters:
      errorCode - error code | 错误码
      message - error message | 错误消息
      unmatchedValue - the value that was not matched | 未匹配的值
  • Method Details

    • unmatchedValue

      public Object unmatchedValue()
      Get the value that was not matched 获取未匹配的值
      Returns:
      the unmatched value, may be null | 未匹配的值,可能为 null
    • noMatch

      public static OpenMatchException noMatch(Object value)
      Create exception for no matching pattern 为没有匹配模式创建异常
      Parameters:
      value - the value that was not matched | 未匹配的值
      Returns:
      exception instance | 异常实例
    • exhaustive

      public static OpenMatchException exhaustive(Object value, Class<?> sealedType)
      Create exception for non-exhaustive sealed type matching 为密封类型非完备匹配创建异常
      Parameters:
      value - the value that was not matched | 未匹配的值
      sealedType - the sealed type being matched | 被匹配的密封类型
      Returns:
      exception instance | 异常实例
    • guardFailed

      public static OpenMatchException guardFailed(Object value)
      Create exception for guard condition failure 为守卫条件失败创建异常
      Parameters:
      value - the value that failed the guard | 守卫失败的值
      Returns:
      exception instance | 异常实例
    • typeMismatch

      public static OpenMatchException typeMismatch(Object value, Class<?> expectedType)
      Create exception for type mismatch 为类型不匹配创建异常
      Parameters:
      value - the actual value | 实际值
      expectedType - the expected type | 期望的类型
      Returns:
      exception instance | 异常实例