增加了一个工具方法CheckedUtil,方便的执行会抛出受检查类型异常的方法调用或者代码段。该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个标准的java8 functional 对象。

This commit is contained in:
wangyao
2021-12-23 18:05:49 +08:00
parent ce504c2a3e
commit d69a9b48df
2 changed files with 381 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package cn.hutool.core.exceptions;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
/**
* 方便的执行会抛出受检查类型异常的方法调用或者代码段
* <p>
* 该工具通过函数式的方式将那些需要抛出受检查异常的表达式或者代码段转化成一个标准的java8 functional 对象
* </p>
*
* @author conder
*/
public class CheckedUtilTest {
@Test
public void supplierTest() {
File noFile = new File("./no-file");
try {
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
FileInputStream stream = CheckedUtil.uncheck(() -> new FileInputStream(noFile)).get();
} catch (Exception re) {
Assert.assertTrue(re instanceof RuntimeException);
}
}
@Test
public void functionTest() {
CheckedUtil.MorFunction<String, String> afunc = (funcParam) -> {
if (funcParam.length() > 5) {
throw new Exception("这是受检查异常需要屌用处显示处理");
}
return funcParam.toUpperCase();
};
//afunc.apply("hello world"); 直接调用需要处理异常
try {
//本行代码原本需要抛出受检查异常,现在只抛出运行时异常
String reslut = CheckedUtil.uncheck(afunc).apply("hello world");
} catch (Exception re) {
Assert.assertTrue(re instanceof RuntimeException);
}
}
}