try-catch-finally处理流中的异常

import java.io.FileWriter;
import java.io.IOException;

public class Demo06Write_try_catch_finally {
    public static void main(String[] args)  {
        FileWriter fw=null;
        try{
            fw=new FileWriter("g:\\basic\\untitled13\\src\\it\\cast\\day15\\demo01\\4.txt",true);
            for (int i = 0; i <10 ; i++) {
                fw.write("Hello"+i+"\r\n");
            }
        }catch (IOException e){
            System.out.println(e);
        }finally {
            //null空指针不能抛出异常的
            if (fw!=null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

posted @ 2020-10-07 17:21  159566  阅读(117)  评论(0编辑  收藏  举报