Skip to content

Commit 4a6b462

Browse files
committed
feat(core): ExceptionUtils 新增 exToThrow 方法
1 parent 2fdd5b6 commit 4a6b462

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

continew-starter-core/src/main/java/top/continew/starter/core/util/ExceptionUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.concurrent.ExecutionException;
2525
import java.util.concurrent.Future;
2626
import java.util.function.Consumer;
27+
import java.util.function.Function;
2728

2829
/**
2930
* 异常工具类
@@ -107,6 +108,25 @@ public static <T> T exToDefault(ExSupplier<T> exSupplier, T defaultValue) {
107108
return exToDefault(exSupplier, defaultValue, null);
108109
}
109110

111+
/**
112+
* 如果有异常,抛出自定义异常
113+
*
114+
* @param exSupplier 可能会出现异常的方法执行
115+
* @param exceptionMapper 异常转换函数
116+
* @param <T> 返回值类型
117+
* @param <E> 自定义异常类型
118+
* @return 执行结果
119+
* @throws E 自定义异常
120+
*/
121+
public static <T, E extends Exception> T exToThrow(ExSupplier<T> exSupplier,
122+
Function<Exception, E> exceptionMapper) throws E {
123+
try {
124+
return exSupplier.get();
125+
} catch (Exception e) {
126+
throw exceptionMapper.apply(e);
127+
}
128+
}
129+
110130
/**
111131
* 如果有异常,执行异常处理,返回默认值
112132
*
@@ -140,5 +160,6 @@ public interface ExSupplier<T> {
140160
* @throws Exception /
141161
*/
142162
T get() throws Exception;
163+
143164
}
144165
}

0 commit comments

Comments
 (0)