Java学习笔记32:finally语句

package Demo_exception;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; 

public class FinallyTest {

	public static void main(String[] args) {
		FileInputStream fis=null;
		System.out.println("---关闭文件---");
		try{
			fis=new FileInputStream("file.text");
		}catch(FileNotFoundException e){
			System.out.println("catch:文件不存在异常!");
			e.printStackTrace();
		}finally{
			System.out.println("执行finally语句");
			try{
				if(fis!=null){
					fis.close();
				}
			}catch(IOException e){//是否被其他程序读写
				System.out.println("文件关闭异常");
				e.printStackTrace();
				}
			}
		}
	}

猜你喜欢

转载自blog.csdn.net/qq_30242987/article/details/85855131