【发布时间】:2018-12-30 19:06:53
【问题描述】:
我正在使用“ObjectInputStream”来加载序列化的二维字符串数组。
问题是,除非我为 ClassNotFoundException 设置特殊的捕获条件,否则我的 IDE Intellij-IDEA 会抛出错误。但是,当我这样做时,它会建议“'catch' branch identical to 'IOException' branch”。
我不知道这意味着我应该做什么。
如何加载序列化的对象而不得到建议或错误?
我的代码:
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;
}
【问题讨论】:
-
catch (IOException | ClassNotFoundException e)并删除第二个catch块 -
@guleryuz 谢谢!这样可行!如果你把它放在答案中,我会接受。
-
这不是编译错误或警告,而是 IDEA 关于如何改进代码片段的建议。
-
那个警告说你的 jdk >1.7 所以使用 multi catch 块而不是分支@LuminousNutria
-
@AndrewTobilko 哦,我想我不知道如何区分。对于那个很抱歉。我会改写我的问题来解释它。
标签: java intellij-idea serialization exception-handling try-catch