Java--finally

finally 子句(clause)是不是总会执行???

package com.volshell.test;



public class Main {

    public static void main(String[] args) {

        change1(10);

    }



    private static void change1(int word) {

        System.out.println("测试结果:" + test(1));

    }



    private static int test(int i) {

        if (i == 1)

            return 0;

        System.out.println("将要进入try块");

        try {

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

            return 3;

        } catch (Exception e) {

            // TODO: handle exception

        } finally {

            System.out.println("finally");

            i++;

            return i;

        }

    }

}

上面结果为 测试结果:0

第一条:如果没有进入try中,是不会执行finally子句的。

 1 package com.volshell.test;

 2 

 3 public class Main {

 4     public static void main(String[] args) {

 5         change1(10);

 6     }

 7 

 8     private static void change1(int word) {

 9         System.out.println("测试结果:" + test(1));

10     }

11 

12     private static int test(int i) {

13 //        if (i == 1)

14 //            return 0;

15         System.out.println("将要进入try块");

16         try {

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

18             System.exit(0);

19             return 3;

20         } catch (Exception e) {

21             // TODO: handle exception

22         } finally {

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

24             i++;

25             return i;

26         }

27     }

28 }

测试结果:将要进入try块
      try block
这次同样没有进入finally中。因为在try中调用了System.exit(0);

第二条:当一个线程正在执行try语句块或者catch语句块的时候,突然被打断或者终止,那么相应的finally是不会被执行的。

***********************************************************************************************************

The finally Block
The finally block always executes when the try block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. But finally is useful for
more than just exception handling — it allows the programmer to avoid having cleanup
code accidentally bypassed by a return,continue, or break. Putting cleanup code in a
finally block is always a good practice, even when no exceptions are anticipated.
Note: If the JVM exits while the try or catch code is being executed, then the finally
block may not execute. Likewise, if the thread executing the try or catch code is
interrupted or killed, the finally block may not execute even though the application
as a whole continues.

***************************************************************************************************************

关于try,catch,finally的执行顺序的问题:

***************************************************************************************************************

where either at least one catch clause, or the finally clause, must be present. 至少有一个catch或者finally子句。可以没有catch

The body of the try statement is executed until either an exception is thrown or the body
finishes successfully.   ---异常抛出或者程序正确执行都会执行try.

If an exception is thrown, each catch clause is examined in turn,
from first to last, to see whether the type of the exception object is assignable to
the type declared in the catch. When an assignable catch clause is found, its block
is executed with its identifier set to reference the exception object. No other catch
clause will be executed. Any number of catch clauses, including zero, can be associated
with a particular Try as long as each clause catches a different type of exception.
If no appropriate catch is found, the exception percolates (渗透)out of the try statement
into any outer try that might have a catch clause to handle it.--如果没有找到合适的捕获,交由外面的捕获来处理

If a finally clause is present with a try, its code is executed after all other processing
in the try is complete. This happens no matter how completion was achieved, whether
normally, through an exception, or through a control flow statement such as return or
break.只有处理完毕try中的语句(不包括异常语句,return语句,break语句)之后,才会执行finally全部子句(包括其中的return子句).

***************************************************************************************************************

第三条:只有处理完毕try中的语句(不包括异常语句,return语句,break语句)之后,才会执行finally全部子句(包括其中的return子句等)。处理完finally之后再返回去处理try中的剩余子句。

你可能感兴趣的:(finally)