import java.io.FileInputStream;
/*
-
try-catch的完整结构
-
try{}
-
catch(异常变量 变量){}
-
catch(异常类型 变量){}
-
catch(异常类型 变量){}
-
finally{}
-
格式注意:
-
try只能有一个。
-
catch可以出现0-n个。
-
finally可以出现0-1个。
-
finally代码块,无论程序出现什么问题最终都会执行一次。
-
资源的释放操作,以便节约内存的性能。
-
finall关闭资源的小结:
-
1.资源应该定义在try{}上面
-
2.finall关闭资源要进行非NULL校验,然后依然要处理编译时异常。
-
*/
public class _Demo07Finally代码块 {public static void main(String[] args) {
FileInputStream fis=null;
try{fis=new FileInputStream("C:user/"); int c=10/0; System.out.println(c); } catch (Exception e){ e.printStackTrace(); System.out.println("异常"); } finally { System.out.println("==最后执行=="); try{ if(fis!=null) fis.close(); } catch (Exception e){ e.printStackTrace(); } }
}
}