最佳答案
您不能将reader.close()
放在try
和catch
之间。可以将其放在 finally
block中,也可以使用 try-with-resources
。喜欢,
try (BufferedReader reader = new BufferedReader(new FileReader(filenameIn))) {
reader.readLine();
for (int i = 0; i < personArray.length; i++) {
String[] data = reader.readLine().split("/t"); // <-- should be \\t for tab.
personArray[i] = new Person(Integer.parseInt(data[0]), data[1],
data[2], Integer.parseInt(data[3]));
}
} catch (IOException e) {
System.out.println("ERROR: WRONG FILE " + e.toString());
} catch (Exception e) {
System.out.println("ERROR" + e.toString());
}
finally
块,BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filenameIn));
reader.readLine();
for (int i = 0; i < personArray.length; i++) {
String[] data = reader.readLine().split("\\t"); // <-- to split on tab.
personArray[i] = new Person(Integer.parseInt(data[0]),
data[1], data[2], Integer.parseInt(data[3]));
}
} catch (IOException e) {
System.out.println("ERROR: WRONG FILE " + e.toString());
} catch (Exception e) {
System.out.println("ERROR" + e.toString());
} finally {
if (reader != null) {
reader.close();
}
}
关于java - Java “try without catch”和 “catch without try” ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43441060/
相关文章:
python - 在 try/except block 中创建变量是否被认为是一种不好的做法?
exception - 如何使用动态变量和中止控制在方案中定义异常?
java - 如何制作检查输入验证的 do-while 循环
java - 快速修复/J : Enable protocols and ciphersuite setting
java - 是否适合在 Java 中对从数据库检索的一小部分数据执行连接操作
java - 从 CalendarView 获取自 UNIX 纪元以来的毫秒时间
java - 将 super/this 构造函数调用中抛出的异常包装到其他异常中
c++ - 这是我第一次编程,我无法运行我的代码
haskell - 专门化未使用的类型变量时不可触及的类型
C++变量的多个定义