接口AutoClosable
有以下方法声明:
void close() throws Exception
因此我们看到方法close可以抛出异常。
当我编写代码尝试使用资源时,它看起来像这样:
private static void printFileJava7() throws IOException {
try(FileInputStream input = new FileInputStream("file.txt")) {
int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
}
}
在这段代码中没有异常处理。
我不明白如果 close 方法抛出异常会发生什么。