Class OpenExpressionException

All Implemented Interfaces:
Serializable
Direct Known Subclasses:
ParserException, SandboxException

public class OpenExpressionException extends OpenException
Expression Exception 表达式异常

Base exception for all expression-related errors including parsing, evaluation, type conversion, and security violations.

所有表达式相关错误的基础异常,包括解析、求值、类型转换和安全违规。

Features | 主要功能:

  • Rich error context with expression string and position - 丰富的错误上下文,包含表达式字符串和位置
  • Static factory methods for common error types - 常见错误类型的静态工厂方法
  • Supports parse, evaluation, type, property, method, and security errors - 支持解析、求值、类型、属性、方法和安全错误

Usage Examples | 使用示例:

// Catch expression errors
try {
    OpenExpression.eval("invalid ++ expr");
} catch (OpenExpressionException e) {
    String expr = e.getExpression();
    int pos = e.getPosition();
}

// Create specific error types
throw OpenExpressionException.parseError("Unexpected token", "1 ++ 2", 2);
throw OpenExpressionException.divisionByZero();

Security | 安全性:

  • Thread-safe: Yes, immutable after construction - 线程安全: 是,构造后不可变
  • Null-safe: Yes, null expression/position handled - 空值安全: 是,null表达式/位置已处理
Since:
JDK 25, opencode-base-expression V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • OpenExpressionException

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

      public OpenExpressionException(String message, Throwable cause)
      Create exception with message and cause 创建带消息和原因的异常
      Parameters:
      message - the error message | 错误消息
      cause - the cause | 原因
    • OpenExpressionException

      public OpenExpressionException(String message, String expression, int position)
      Create exception with expression context 创建带表达式上下文的异常
      Parameters:
      message - the error message | 错误消息
      expression - the expression string | 表达式字符串
      position - the error position | 错误位置
  • Method Details

    • getExpression

      public String getExpression()
      Get expression string 获取表达式字符串
      Returns:
      the expression | 表达式
    • getPosition

      public int getPosition()
      Get error position 获取错误位置
      Returns:
      the position | 位置
    • parseError

      public static OpenExpressionException parseError(String message, String expression, int position)
      Create parse error 创建解析错误
      Parameters:
      message - the error message | 错误消息
      expression - the expression | 表达式
      position - the position | 位置
      Returns:
      the exception | 异常
    • parseError

      public static OpenExpressionException parseError(String message, int position)
      Create parse error with position only 创建只带位置的解析错误
      Parameters:
      message - the error message | 错误消息
      position - the position | 位置
      Returns:
      the exception | 异常
    • evaluationError

      public static OpenExpressionException evaluationError(String message, Throwable cause)
      Create evaluation error 创建求值错误
      Parameters:
      message - the error message | 错误消息
      cause - the cause | 原因
      Returns:
      the exception | 异常
    • evaluationError

      public static OpenExpressionException evaluationError(String message)
      Create evaluation error 创建求值错误
      Parameters:
      message - the error message | 错误消息
      Returns:
      the exception | 异常
    • typeError

      public static OpenExpressionException typeError(String expected, Object actual)
      Create type error 创建类型错误
      Parameters:
      expected - the expected type | 期望类型
      actual - the actual value | 实际值
      Returns:
      the exception | 异常
    • propertyNotFound

      public static OpenExpressionException propertyNotFound(String property, Class<?> type)
      Create property not found error 创建属性未找到错误
      Parameters:
      property - the property name | 属性名
      type - the target type | 目标类型
      Returns:
      the exception | 异常
    • methodNotFound

      public static OpenExpressionException methodNotFound(String method, Class<?> type)
      Create method not found error 创建方法未找到错误
      Parameters:
      method - the method name | 方法名
      type - the target type | 目标类型
      Returns:
      the exception | 异常
    • functionNotFound

      public static OpenExpressionException functionNotFound(String function)
      Create function not found error 创建函数未找到错误
      Parameters:
      function - the function name | 函数名
      Returns:
      the exception | 异常
    • securityViolation

      public static OpenExpressionException securityViolation(String message)
      Create security violation error 创建安全违规错误
      Parameters:
      message - the error message | 错误消息
      Returns:
      the exception | 异常
    • timeout

      public static OpenExpressionException timeout(long millis)
      Create timeout error 创建超时错误
      Parameters:
      millis - the timeout in milliseconds | 超时毫秒数
      Returns:
      the exception | 异常
    • divisionByZero

      public static OpenExpressionException divisionByZero()
      Create division by zero error 创建除零错误
      Returns:
      the exception | 异常
    • nullPointer

      public static OpenExpressionException nullPointer(String context)
      Create null pointer error 创建空指针错误
      Parameters:
      context - the context description | 上下文描述
      Returns:
      the exception | 异常