Class OpenException

All Implemented Interfaces:
OpenExceptionMeta, Serializable
Direct Known Subclasses:
OpenIOException, OpenTimeoutException

public class OpenException extends RuntimeException implements OpenExceptionMeta
OpenException - Unified exception base class for OpenCode components OpenException - OpenCode 组件统一异常基类

Base exception for all OpenCode components with error code and component name support.

所有组件异常都应继承此类,支持错误码和组件名称,便于异常分类和定位。

Naming convention / 命名约定: When an Open*Exception is conceptually "an upgraded drop-in replacement" for a specific JDK-native exception, it MUST extend that JDK-native type (not OpenException), keeping JDK idioms working (catch, instanceof, AssertJ isInstanceOf). The trade-off — catch (OpenException e) no longer catches these variants — is intentional; use OpenExceptionMeta for unified metadata access across both branches (see OpenExceptionMeta Javadoc).

Open*Exception 在语义上是某个 JDK 原生异常的"增强替代"时, 它必须继承对应 JDK 原生类(而非 OpenException), 保持 JDK 习惯(catchinstanceof、AssertJ isInstanceOf)依然生效。代价是 catch (OpenException e) 无法捕获这些变种——这是刻意为之;如需跨两条继承链统一访问 metadata,请使用 OpenExceptionMeta

Alignment table / 对齐表:

Features | 主要功能:

  • Error code support - 错误码支持
  • Component name support - 组件名称支持
  • Formatted message output [Component] (Code) Message - 格式化消息输出
  • RuntimeException (unchecked) - 非受检异常

Usage Examples | 使用示例:

throw new OpenException("Operation failed");
throw new OpenException("Core", "CORE_001", "Parameter required");
throw new OpenException("IO failed", cause);

Security | 安全性:

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

    • OpenException

      public OpenException(String message)
      Creates 创建异常
      Parameters:
      message - the value | 异常消息
    • OpenException

      public OpenException(String message, Throwable cause)
      Creates 创建异常(带原因)
      Parameters:
      message - the value | 异常消息
      cause - the value | 原始异常
    • OpenException

      public OpenException(String component, String errorCode, String message)
      Creates 创建异常(带组件和错误码)
      Parameters:
      component - the value | 组件名称
      errorCode - the value | 错误码
      message - the value | 异常消息
    • OpenException

      public OpenException(String component, String errorCode, String message, Throwable cause)
      Creates 创建异常(完整参数)
      Parameters:
      component - the value | 组件名称
      errorCode - the value | 错误码
      message - the value | 异常消息
      cause - the value | 原始异常
  • Method Details

    • getErrorCode

      public String getErrorCode()
      Gets 获取错误码
      Specified by:
      getErrorCode in interface OpenExceptionMeta
      Returns:
      the result | 错误码,可能为 null
    • getComponent

      public String getComponent()
      Gets 获取组件名称
      Specified by:
      getComponent in interface OpenExceptionMeta
      Returns:
      the result | 组件名称,可能为 null
    • getRawMessage

      public String getRawMessage()
      Gets 获取原始消息(不含组件和错误码前缀)
      Returns:
      the result | 原始消息
    • getMessage

      public String getMessage()
      Gets 获取格式化的异常消息

      格式: [组件] (错误码) 消息

      Overrides:
      getMessage in class Throwable
      Returns:
      the result | 格式化的消息