finally代码块:
必须执行的代码块,不管是否有异常产生,即使发生OutOfMemoryError也会执行,通常用于执行善后清理工作。如果finally代码块没有执行,那么有三种情况可能:
1、没有进入try代码块。
2、进入try代码块,但是代码运行中出现了死循环或死锁情况。
3、进入try代码块,但是执行了System.exit()操作。
注意,finally是在return表达式运行后执行的,此时将要
return的结果已经暂存起来,待finally代码块执行结束后
再将之前暂存的结果返回,实例代码如下:
public
static
void main(String[]
args) {
int
result =
finallyNotWork();
System.
out.println(
result);
// 10001
}
public
static
int finallyNotWork() {
int
temp = 10000;
try {
throw
new Exception();
}
catch (Exception
e) {
return ++
temp;
}
finally {
temp = 99990;
}
}
finally代码块的职责不在于对变量进行赋值等操作,而是清理资源、释放连接、关闭管道流等操作。
相对在finally代码块中赋值,更加危险的做法是在finally块中使用return操作,这样的代码会使返回值变得非常不可控,警示代码如下:
public
class TryCatchFinally {
static
int
x = 1;
static
int
y = 10;
static
int
z = 100;
public
static
void main(String[]
args) {
int
value =
finallyReturn();
System.
out.println(
"value=" +
value);
System.
out.println(
"x=" +
x);
System.
out.println(
"y=" +
y);
System.
out.println(
"z=" +
z);
}
private
static
int finallyReturn() {
try {
// ...
return
++
x
;
}
catch (Exception
e) {
return
++
y
;
}
finally
{
return
++
z
;
}
}
}
执行结果如下:
value=101
x=2
y=10
z=101
以上执行结果说明:
1、最后return动作是由finally代码块中的return ++z完成的,所以方法返回的结果是101。
2、语句return ++x中的++x被成功的执行,所以运行结果是x=2。
3、如果有异常抛出,那么运行结果将会是y=11,而x=1。
finally代码块中使用return语句,使返回值的判断变得复杂,所以避免返回值不可控,我们不要在finally代码块中使用return语句。
Class Test{
public static String output="";
public static void foo(int i){
try{
if(i==1){
throw new Exception();
}
output+="1";
} catch(Exception e){
output+="2";
return;
} finally{
output+="3";
}
output+="4";
}
public static void main(String args[]){
foo(0);
System.out.println(output);//134
foo(1);
System.out.println(output);//13423
}
}