java进入finally_Java--finally

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

package com.volshell.test;public classMain {public static voidmain(String[] args) {

change1(10);

}private static void change1(intword) {

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

}private static int test(inti) {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++;returni;

}

}

}

上面结果为 测试结果:0

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

1 package com.volshell.test;2

3 public classMain {4 public static voidmain(String[] args) {5 change1(10);6 }7

8 private static void change1(intword) {9 System.out.println("测试结果:" + test(1));10 }11

12 private static int test(inti) {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 returni;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中的剩余子句。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值