还有这种写法
try-with-resources
try(FileInputStream is =new FileInputStream("sample.txt")) {....}
package cn.itcast.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TryTest9 {
public static void main(String[] args) {
FileInputStream is = null;
try {
is = new FileInputStream("sample.txt");
throw new FileNotFoundException();
} catch (Exception e) {
System.out.println("A");
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("B");
}
}