From 4a6b4624c2ed769bba6c50efd90592f7719247e5 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 21 May 2025 22:18:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20ExceptionUtils=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20exToThrow=20=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/core/util/ExceptionUtils.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/continew-starter-core/src/main/java/top/continew/starter/core/util/ExceptionUtils.java b/continew-starter-core/src/main/java/top/continew/starter/core/util/ExceptionUtils.java index fc7783dd..612b700b 100644 --- a/continew-starter-core/src/main/java/top/continew/starter/core/util/ExceptionUtils.java +++ b/continew-starter-core/src/main/java/top/continew/starter/core/util/ExceptionUtils.java @@ -24,6 +24,7 @@ import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.function.Consumer; +import java.util.function.Function; /** * 异常工具类 @@ -107,6 +108,25 @@ public class ExceptionUtils { return exToDefault(exSupplier, defaultValue, null); } + /** + * 如果有异常,抛出自定义异常 + * + * @param exSupplier 可能会出现异常的方法执行 + * @param exceptionMapper 异常转换函数 + * @param 返回值类型 + * @param 自定义异常类型 + * @return 执行结果 + * @throws E 自定义异常 + */ + public static T exToThrow(ExSupplier exSupplier, + Function exceptionMapper) throws E { + try { + return exSupplier.get(); + } catch (Exception e) { + throw exceptionMapper.apply(e); + } + } + /** * 如果有异常,执行异常处理,返回默认值 * @@ -140,5 +160,6 @@ public class ExceptionUtils { * @throws Exception / */ T get() throws Exception; + } }