finally{} 代码块

 1 package Exception;
 2 /*
 3  * finally{}代码块
 4  *
 5  * finally{]代码块是必须要被执行的,不管异常是否处理成功,该代码块里面的代码体都会被执行,
 6  */
 7 public class FinallyException {
 8     public static void main(String[] args) {
 9         try{
10             div(4, 0);
11         }catch(ArithmeticException ae){
12             System.out.println("出现了算术异常!!!");
13         }finally{   //finally代码块是必须处理的
14             System.out.println("异常处理完毕了!!!");
15         }
16     }
17     public static int div(int a, int b)throws ArithmeticException{
18         return a / b;
19     }

 finally 代码块主要用来释放资源,比如说   I/O 缓冲区 数据库连接。

原文地址:https://www.cnblogs.com/the-wang/p/6923176.html