Java 异常处理(1) : try-catch- finally中finally的使用

  1 package com.bytezero.throwable;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileNotFoundException;
  6 import java.io.IOException;
  7 
  8 import org.junit.Test;
  9 
 10 /**
 11  * 
 12  * @Description       try-catch- finally中finally的使用:
 13  * @author Bytezero·zhenglei!        Email:420498246@qq.com
 14  * @version
 15  * @date 上午9:25:36
 16  * @     1.finally是可选的
 17  *       2.finally中声明的是一定会被执行的代码,即使catch中又出现异常了,try中
 18  *       有return语句,catch中有return语句等情况。
 19  *       3.像数据库连接,输入输出流,网络编程Socket等资源,JVM是不能自动回收的,
 20  *       我们需要自己手动的进行资源的释放,此时的资源释放,就需要声明在finally中。
 21  *       
 22  *       
 23  *
 24  */
 25 public class FinallyTest {
 26     
 27     @Test
 28     public void test2() {
 29         FileInputStream fis = null;
 30         try {
 31             File file = new File("hello.txt");
 32             fis = new FileInputStream(file);
 33             
 34             int data = fis.read();
 35             while(data != -1) {
 36                 System.out.print((char)data);
 37                 data = fis.read();
 38                 
 39             }
 40             
 41         } catch (FileNotFoundException e) {
 42             // TODO Auto-generated catch block
 43             e.printStackTrace();
 44         } catch (IOException e) {
 45             // TODO Auto-generated catch block
 46             e.printStackTrace();
 47         }finally {
 48             
 49             try {
 50                 if(fis != null)
 51                     fis.close();
 52             } catch (IOException e) {
 53                 // TODO Auto-generated catch block
 54                 e.printStackTrace();
 55             } 
 56         }
 57     }
 58     
 59     
 60     @Test
 61     public void testMethod(){
 62         int num = method();
 63         System.out.println(num);
 64         
 65         
 66     }
 67     public int method() {
 68         
 69         try {
 70             
 71             int[] arr = new int[10];
 72             //System.out.println(arr[10]);
 73             return 1;
 74         }catch(ArrayIndexOutOfBoundsException e) {
 75             e.printStackTrace();
 76             return 2;
 77         }finally {
 78             System.out.println("我一定会被执行");
 79             //return 3;
 80         }
 81         
 82     }
 83     
 84     
 85     
 86     @Test
 87     public void test1() {
 88         
 89         try {
 90                 int a = 10;
 91                 int b = 0;
 92                 System.out.println(a / b);
 93                 
 94         
 95         }catch(ArithmeticException e) {
 96             //e.printStackTrace();
 97             
 98             int[] arr = new int[10];
 99             System.out.println(arr[10]);
100             
101         }catch(Exception e) {
102             e.printStackTrace();
103         }
104         //System.out.println("我 好帅啊~~");
105         
106         finally {
107             
108             System.out.println("我 好帅啊~~");
109         }
110         
111         
112     }
113 
114 }

 

 

 

 

 

 

posted on 2021-10-09 10:24  Bytezero!  阅读(49)  评论(0编辑  收藏  举报