1.7改进
有两个改进
1 多个异常可以再一个catch中同时捕捉, 使用 | 隔开
2 自动关闭资源
try(开启资源语句){ 操作; }catch(异常类型 变量){ 处理代码; }
public class Exception_10 {
public static void main(String[] args) {
// 传统关闭资源写法
FileInputStream fis = null;
try {
fis = new FileInputStream("D:/123.txt");
//TODO 数据操作
System.out.println(fis.read());
//fis.close();
}catch (IOException e ) {
e.printStackTrace();
}finally {
try {
if(fis != null) {
fis.close();}
}catch(IOException e) {
e.printStackTrace();
}
}
//新特性写法,自动关闭资源
try(FileInputStream fis1 = new FileInputStream("D:/123.txt");){
//TODO 操作
System.out.println(fis1.read() + "--");
}catch(Exception e) {
//TODO:handle exception
}
}
}