Java的finally理解

时间:2023-03-10 06:49:17
Java的finally理解

1.为什么要用finally

先看一个没有finally的异常处理try-catch语句:

如果count为要使用到的资源,而且用完要求释放此资源。那么我们能够把释放资源的语句放到try-catch后运行,当前的程序无论是在运行完try语句块还是catch语句块,都会顺序运行到以下释放资源的语句。

int count = 0;  //初始化资源

try{

  count++;

  if(count == 1) throw new Exception("Exception in try");

}catch(Exception e){

  System.out.println("catch block");

}

count = 0; //释放资源

可是,假设在try或catch中有多条return语句,那么在每条return语句之前,都要先运行释放资源的语句:

public void f() throws Exception {

 int count = 0;  //初始化资源

 try{

   doSomething;

   statementMayCauseException;  //可能会抛出异常的语句,若异常没有被catch,则直接抛出,也不会运行到try-catch以下的语句

   doSomething;

   if(count == 1) throw new Exception1("E1 in try");

   if(count == 2) throw new Exception2("E2 in try");

 }catch(Exception1 e){

   count = 0; //释放资源

   throw e;  //再次把异常抛出,让上一级捕获。此时将不会运行catch外的语句,所以要先释放资源

 }catch(Exception2 e){

  count = 0; //释放资源

  return; //返回了,也不会运行catch外的语句,所以要先释放资源

 }

 count = 0; //释放资源

}

这样,就须要在每个可能返回的地方,以及每个可能出现异常而导致程序跳转的地方,考虑怎样释放资源,导致复杂和冗余。

所以,须要finally语句。

把资源释放或状态还原的代码放到finally块中,能够保证在try和catch语句运行完后,一定会运行finally语句块,而不用考虑各种复杂的跳转情况。

int count = 0;

try{

 count++;

 if(count == 1)throw new Exception();

}catch(Exception e){

}finally{

 count = 0;

}

2.finally什么时候运行

finally在return语句之后,跳转到上一级程序之前运行。

public class Test {

 public static void main(String[] args) { 

  System.out .println(test ()); 

 }  

 public static String test() { 

  try { 

   System.out .println("try block"); 

   return test1 (); 

  } finally { 

   System.out .println("finally block");

   //return "finally";   

  } 

 } 

 public static String test1() { 

  System.out .println("return statement"); 

  return "after return"; 

 } 

}

结果:

try block

return statement

finally block

after return

分析:

1.try语句块,return test1(),则调用test1方法

2.test1()运行后返回"after return",返回值"after return"保存在一个暂时区域里

3.运行finally语句块。若finally语句有返回值,则此返回值将替换掉暂时区域的返回值

4.将暂时区域的返回值送到上一级方法中。

參考:

《thinking in Java》

http://blog.****.net/jiasanshou/article/details/6996932

以上为个人理解,若有不正确之处,望不吝指正。