public class Exceptions extends Object
该类包含以下功能:
使用示例:
// 获取异常的根因 Throwable rootCause = Exceptions.getRootCause(exception); // 静默执行代码,忽略可能的异常 String result = Exceptions.getQuietly(() -> someMethodThatMayThrow()); // 检查异常是否属于指定类型 boolean isIOException = Exceptions.matches(exception, IOException.class);
| 构造器和说明 |
|---|
Exceptions() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <T> T |
callQuietly(Callable<T> callable)
静默执行Callable,忽略可能发生的异常
|
static <T> T |
callQuietly(Callable<T> callable,
Consumer<Throwable> consumer)
静默执行Callable,使用指定的消费者处理可能发生的异常
|
static <T> T |
callQuietly(Callable<T> callable,
Throwable t)
静默执行Callable,将可能发生的异常添加为指定Throwable的抑制异常
|
static void |
executeQuietly(Executable executable)
静默执行Executable,忽略可能发生的异常
|
static void |
executeQuietly(Executable executable,
Consumer<Throwable> consumer)
静默执行Executable,使用指定的消费者处理可能发生的异常
|
static void |
executeQuietly(Executable executable,
Throwable t)
静默执行Executable,将可能发生的异常添加为指定Throwable的抑制异常
|
static Set<Throwable> |
getCausePath(Throwable t)
获取Throwable的因果链路径
|
static <T> T |
getQuietly(Supplier<T> supplier)
静默获取Supplier的值,忽略可能发生的异常
|
static <T> T |
getQuietly(Supplier<T> supplier,
Consumer<Throwable> consumer)
静默获取Supplier的值,使用指定的消费者处理可能发生的异常
|
static <T> T |
getQuietly(Supplier<T> supplier,
Throwable t)
静默获取Supplier的值,将可能发生的异常添加为指定Throwable的抑制异常
|
static Throwable |
getRootCause(Throwable t)
获取Throwable的根因异常
|
static Throwable |
getRootCauseSafely(Throwable t)
安全地获取Throwable的根因异常,防止循环引用
|
static Throwable |
getRootCauseSafelyUntil(Throwable t,
Predicate<Throwable> predicate)
安全地获取Throwable的根因异常,直到满足指定条件为止,防止循环引用
|
static Throwable |
getRootCauseUntil(Throwable t,
Predicate<Throwable> predicate)
获取Throwable的根因异常,直到满足指定条件为止
|
static String |
getStackTrace(Throwable t)
获取Throwable的堆栈跟踪信息
|
static Throwable |
hasCause(Throwable t,
Class<? extends Throwable> type)
检查Throwable或其因果链中是否存在指定类型的异常
|
static Throwable |
hasCause(Throwable t,
Predicate<Throwable> predicate)
检查Throwable或其因果链中是否存在满足条件的异常
|
static boolean |
matches(Throwable throwable,
boolean matchIfEmpty,
Class<? extends Throwable>... targets)
检查抛出的异常是否与目标异常类型列表中的任何一个匹配
|
static boolean |
matches(Throwable throwable,
Class<? extends Throwable>... targets)
检查抛出的异常是否与目标异常类型列表中的任何一个匹配
|
static <T extends Exception> |
of(Throwable t,
Class<T> type,
Function<Throwable,T> builder)
从Throwable中查找指定类型的异常,如果找不到则使用Function构建器创建新的异常实例
|
static <T extends Exception> |
of(Throwable t,
Class<T> type,
Supplier<T> builder)
从Throwable中查找指定类型的异常,如果找不到则使用Supplier构建器创建新的异常实例
|
static void |
quietlyClose(AutoCloseable closeable)
静默关闭AutoCloseable资源,忽略可能发生的异常
|
static void |
quietlyClose(AutoCloseable closeable,
Throwable t)
静默关闭AutoCloseable资源,将可能发生的异常添加为指定Throwable的抑制异常
|
static void |
runQuietly(Runnable runnable)
静默执行Runnable,忽略可能发生的异常
|
static void |
runQuietly(Runnable runnable,
Consumer<Throwable> consumer)
静默执行Runnable,使用指定的消费者处理可能发生的异常
|
static void |
runQuietly(Runnable runnable,
Throwable t)
静默执行Runnable,将可能发生的异常添加为指定Throwable的抑制异常
|
public static <T extends Exception> T of(Throwable t, Class<T> type, Function<Throwable,T> builder)
T - 异常类型t - 要查找的Throwable对象,可以为nulltype - 要查找的异常类型builder - 当找不到指定类型异常时用于创建新异常的函数public static <T extends Exception> T of(Throwable t, Class<T> type, Supplier<T> builder)
T - 异常类型t - 要查找的Throwable对象,可以为nulltype - 要查找的异常类型builder - 当找不到指定类型异常时用于创建新异常的供应器public static Throwable getRootCauseUntil(@Nonnull Throwable t, Predicate<Throwable> predicate)
t - Throwable对象predicate - 判断条件public static Throwable getRootCauseSafelyUntil(@Nonnull Throwable t, Predicate<Throwable> predicate)
t - Throwable对象predicate - 判断条件public static Throwable getRootCause(@Nonnull Throwable t)
t - Throwable对象public static Throwable getRootCauseSafely(@Nonnull Throwable t)
t - Throwable对象public static Set<Throwable> getCausePath(@Nonnull Throwable t)
t - Throwable对象public static Throwable hasCause(@Nonnull Throwable t, Predicate<Throwable> predicate)
t - Throwable对象predicate - 判断条件public static Throwable hasCause(@Nonnull Throwable t, Class<? extends Throwable> type)
t - Throwable对象type - 要检查的异常类型public static String getStackTrace(Throwable t)
t - Throwable对象public static void quietlyClose(AutoCloseable closeable)
closeable - 可关闭的资源public static void quietlyClose(AutoCloseable closeable, Throwable t)
closeable - 可关闭的资源t - 用于添加抑制异常的Throwablepublic static void runQuietly(Runnable runnable)
runnable - 要执行的Runnablepublic static void runQuietly(Runnable runnable, Consumer<Throwable> consumer)
runnable - 要执行的Runnableconsumer - 用于处理异常的消费者public static void runQuietly(Runnable runnable, Throwable t)
runnable - 要执行的Runnablet - 用于添加抑制异常的Throwablepublic static <T> T getQuietly(Supplier<T> supplier)
T - 返回值类型supplier - 提供值的Supplierpublic static <T> T getQuietly(Supplier<T> supplier, Consumer<Throwable> consumer)
T - 返回值类型supplier - 提供值的Supplierconsumer - 用于处理异常的消费者public static <T> T getQuietly(Supplier<T> supplier, Throwable t)
T - 返回值类型supplier - 提供值的Suppliert - 用于添加抑制异常的Throwablepublic static void executeQuietly(Executable executable)
executable - 要执行的Executablepublic static void executeQuietly(Executable executable, Consumer<Throwable> consumer)
executable - 要执行的Executableconsumer - 用于处理异常的消费者public static void executeQuietly(Executable executable, Throwable t)
executable - 要执行的Executablet - 用于添加抑制异常的Throwablepublic static <T> T callQuietly(Callable<T> callable)
T - 返回值类型callable - 要执行的Callablepublic static <T> T callQuietly(Callable<T> callable, Consumer<Throwable> consumer)
T - 返回值类型callable - 要执行的Callableconsumer - 用于处理异常的消费者public static <T> T callQuietly(Callable<T> callable, Throwable t)
T - 返回值类型callable - 要执行的Callablet - 用于添加抑制异常的Throwable@SafeVarargs public static boolean matches(Throwable throwable, Class<? extends Throwable>... targets)
throwable - 要检查的抛出异常targets - 目标异常类型列表@SafeVarargs public static boolean matches(Throwable throwable, boolean matchIfEmpty, Class<? extends Throwable>... targets)
throwable - 要检查的抛出异常matchIfEmpty - 当targets为空时的默认返回值targets - 目标异常类型列表Copyright © 2025 fossc. All rights reserved.