gpt4 book ai didi

java - 如何遵循 IntelliJ-IDEA 关于 try/catch block 的建议 "catch branch identical"?

转载 作者:行者123 更新时间:2023-11-30 01:57:17 31 4
gpt4 key购买 nike

我正在使用“ObjectInputStream”加载序列化的 2D 字符串数组。

问题是,除非我为 ClassNotFoundException 设置特殊的捕获条件,否则我的 IDE(Intellij-IDEA)将抛出错误。但是,当我这样做时,它建议“'catch'分支与'IOException'分支相同”。

我不知道这意味着我应该做什么。

如何加载序列化对象而不会收到建议或错误?

我的代码:

private String[][] getPossArray(String race, boolean isFirstName) {
String[][] retVal = new String[0][];

try {
FileInputStream fis = new FileInputStream("./res/binary_files/Human_FirstNameString[][].ser");
ObjectInputStream ois = new ObjectInputStream(fis);

retVal = (String[][]) ois.readObject();
ois.close();

} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

return retVal;
}

最佳答案

感谢 guleryuz 的评论,我发现 IntelliJ 的建议试图告诉我,我可以通过将 catch block 更改为 catch (IOException | ClassNotFoundException e) 来摆脱建议通知而不是将每个 catch 语句放在自己的行上。

旧的 Catch-Block 版本:

} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

新的 Catch-Block 版本:

} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}

关于java - 如何遵循 IntelliJ-IDEA 关于 try/catch block 的建议 "catch branch identical"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980593/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com