s-day09--JDK7IO异常的处理方式(重点)

This commit is contained in:
xx
2026-10-23 14:16:25 +08:00
parent 5fc0b7e169
commit a60b906e5a
3 changed files with 38 additions and 0 deletions
@@ -0,0 +1,13 @@
package com.inmind.io_exception05;
public class Demo15 {
public static void main(String[] args) {
try (Student s = new Student()){
System.out.println("try中代码执行完毕");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("程序结束");
}
}
@@ -0,0 +1,17 @@
package com.inmind.io_exception05;
import java.io.FileWriter;
//17.JDK7异常的处理方式
public class JDK7_IO_Demo14 {
public static void main(String[] args) {
//JDK7的语法糖,它会主动调用try()中流对象的close方法
try (FileWriter fw = new FileWriter("target.txt")) {
fw.write("这是jdk7的IO异常处理");
fw.flush();
}catch (Exception e){
e.printStackTrace();
}
System.out.println("程序结束");
}
}
@@ -0,0 +1,8 @@
package com.inmind.io_exception05;
public class Student implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("学生类的close方法执行了");
}
}