Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将引发UncheckedIOException,而不是其他预期异常_Java_Oop_Exception_Refactoring_Cactoos - Fatal编程技术网

Java 将引发UncheckedIOException,而不是其他预期异常

Java 将引发UncheckedIOException,而不是其他预期异常,java,oop,exception,refactoring,cactoos,Java,Oop,Exception,Refactoring,Cactoos,在重构以使用而不是时,我遇到了一个问题,即对和的否定测试GithubProfileValidationTest 重构之后,上述两个测试类的肯定测试用例都通过了,但是预期特定异常的否定测试用例失败了。 测试中受影响的重构代码是GithubProfile.assetsmethod和GithubProfile.assetmethod 我将资产方法重构为如下所示: public Map<String, InputStream> assets() throws IOException {

在重构以使用而不是时,我遇到了一个问题,即对和的否定测试
GithubProfileValidationTest

重构之后,上述两个测试类的肯定测试用例都通过了,但是预期特定异常的否定测试用例失败了。 测试中受影响的重构代码是
GithubProfile.assets
method和
GithubProfile.asset
method

我将
资产
方法重构为如下所示:

 public Map<String, InputStream> assets() throws IOException {
    final XML xml = this.read();
    final List<XML> nodes = xml.nodes("/p/entry[@key='assets']/entry");
    return new MapOf<>(
        new Mapped<>(
            nodes,
            input ->
                new MapEntry<>(
                    input.xpath("@key").get(0),
                    this.asset(input.xpath("text()").get(0))
                )
        )
    );
}
public Map assets()引发IOException{
final XML=this.read();
最终列表节点=xml.nodes(“/p/entry[@key='assets']/entry”);
返回新的MapOf(
新映射(
节点,
输入->
新地图条目(
input.xpath(“@key”).get(0),
this.asset(input.xpath(“text()”).get(0))
)
)
);
}
在不同的测试用例中,
this.asset
调用将抛出
Profile.ConfigException
。相反,在调用assets方法时,测试失败,出现
无法计算表达式方法抛出的'java.io.UncheckedIOException'异常
,而
Profile.ConfigException
被忽略/隐藏

似乎
MapOf
以某种方式无法计算或“隐藏”调用this.asset方法引发的异常,使其自身引发了
UncheckedIOException
,因此我无法修复此问题并引发
Profile.ConfigException

调试时,
UncheckedIOException
不包含任何有关引发的
Profile.ConfigException
的信息


有没有关于我为什么会出现这种行为或可能的解决方案的提示?

原因可能是在迭代填充映射时在
org.cactoos.func.UncheckedFunc
中进行了转换


由于函数式编程通常不能很好地处理异常,因此API试图避免声明已检查的异常。因此您可能不得不接受这种情况。

问题在于
Iterable#next()
(在JDK中)不允许抛出选中的异常(如
Profile.ConfigException
)。这就是为什么
org.cactoos.iterator.Mapped
捕获它们并抛出
UncheckedIOException
。多亏了JDK设计,它是不可压缩的。您所能做的最好的事情就是为循环做好旧的

public Map<String, InputStream> assets() throws IOException {
  final XML xml = this.read();
  final List<XML> nodes = xml.nodes("/p/entry[@key='assets']/entry");
  final List<MapEntry> entries = new LinkedList<>();
  for (final XML node : nodes) {
    entries.add(
      new MapEntry<>(
        input.xpath("@key").get(0),
        this.asset(input.xpath("text()").get(0)) // checked exeption here
      )
    );
  }
  return new MapOf<>(entries);
}
public Map assets()引发IOException{
final XML=this.read();
最终列表节点=xml.nodes(“/p/entry[@key='assets']/entry”);
最终列表条目=新建LinkedList();
for(最终XML节点:节点){
条目。添加(
新地图条目(
input.xpath(“@key”).get(0),
this.asset(input.xpath(“text()”).get(0))//选中此处的exeption
)
);
}
返回新的MapOf(条目);
}

我看到的唯一一行代码可以抛出
未选中的异常
这是您的
read()
方法。它有什么作用?