Class ExceptionUtil
java.lang.Object
cloud.opencode.base.core.exception.ExceptionUtil
Exception Utility - Exception chain analysis and handling utilities
异常工具类 - 异常链分析和处理工具
Provides exception chain analysis, stack trace handling and safe execution wrappers.
提供异常链分析、堆栈跟踪处理和安全执行包装器等功能。
Features | 主要功能:
- Root cause extraction (getRootCause) - 根本原因提取
- Stack trace to string (getStackTrace) - 堆栈跟踪转字符串
- Exception chain traversal (getCausalChain) - 异常链遍历
- Checked exception wrapping (wrapAndThrow) - 受检异常包装
- Sneaky throw (bypass compilation check) - 静默抛出
Usage Examples | 使用示例:
Throwable root = ExceptionUtil.getRootCause(exception);
String stackTrace = ExceptionUtil.getStackTrace(exception);
List<Throwable> chain = ExceptionUtil.getCausalChain(exception);
ExceptionUtil.sneakyThrow(new IOException("test"));
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA Runnable that may throw checked exceptions 可抛出受检异常的 Runnablestatic interfaceA Supplier that may throw checked exceptions 可抛出受检异常的 Supplier -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanChecks 判断异常链中是否包含指定类型的异常getCausalChain(Throwable throwable) Gets 获取异常的因果链static StringgetMessage(Throwable throwable) Gets 获取异常消息,如果为 null 则返回异常类名static ThrowablegetRootCause(Throwable throwable) Gets 获取异常的根本原因static StringgetRootCauseMessage(Throwable throwable) Gets 获取根本原因的消息static StringgetStackTrace(Throwable throwable) Gets 获取异常的堆栈跟踪字符串static <T extends Throwable>
RuntimeExceptionsneakyThrow(Throwable throwable) Silently throws a checked exception 静默抛出受检异常static ThrowableUnwraps a nested exception 解包嵌套异常static <T extends Throwable>
TUnwraps an exception of a specific type 解包特定类型的异常static <T> TwrapAndReturn(ExceptionUtil.CheckedSupplier<T> supplier) Wraps 包装受检异常为运行时异常并返回结果static voidwrapAndThrow(ExceptionUtil.CheckedRunnable runnable) Wraps 包装受检异常为运行时异常并抛出
-
Method Details
-
getRootCause
-
getStackTrace
-
getCausalChain
-
unwrap
-
unwrap
Unwraps an exception of a specific type 解包特定类型的异常遍历异常链,找到并返回指定类型的异常。
- Type Parameters:
T- the value | 异常类型- Parameters:
throwable- the exception | 异常exceptionType- the value | 目标异常类型- Returns:
- the result | 找到的异常,如果未找到则返回 null
-
wrapAndThrow
Wraps 包装受检异常为运行时异常并抛出执行可能抛出受检异常的代码,如果发生异常则包装为
OpenException。- Parameters:
runnable- the value | 可能抛出受检异常的代码块- Throws:
OpenException- if the condition is not met | 如果发生异常
-
wrapAndReturn
Wraps 包装受检异常为运行时异常并返回结果- Type Parameters:
T- the value | 返回值类型- Parameters:
supplier- the value | 可能抛出受检异常的代码块- Returns:
- the result | 执行结果
- Throws:
OpenException- if the condition is not met | 如果发生异常
-
sneakyThrow
Silently throws a checked exception 静默抛出受检异常利用泛型擦除机制,将受检异常作为运行时异常抛出, 而不需要在方法签名中声明。
警告
谨慎使用此方法,因为它绕过了编译器的异常检查。 仅在确实需要抛出原始受检异常时使用。- Type Parameters:
T- the value | 伪装的异常类型- Parameters:
throwable- the value | 要抛出的异常- Returns:
- the result | 永不返回,仅用于满足返回类型要求
- Throws:
T- if the condition is not met | 实际抛出的异常
-
contains
-
getMessage
-
getRootCauseMessage
-