try、catch、finally执行顺序
案例一
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
try{
System.out.println("try代码块被执行");
return 1;
}catch(Exception e){
System.out.println("catch代码块被执行");
return 2;
}finally{
System.out.println("finally代码块被执行");
}
}
}
执行结果
try代码块被执行
finally代码块被执行
method代码块中执行结果:1
案例二
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
try{
System.out.println("try代码块被执行");
int i = 1 / 0;
return 1;
}catch(Exception e){
System.out.println("catch代码块被执行");
return 2;
}finally{
System.out.println("finally代码块被执行");
}
}
}
执行结果
try代码块被执行
catch代码块被执行
finally代码块被执行
method代码块中执行结果:2
案例三
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
try{
System.out.println("try代码块被执行");
int i = 1 / 0;
return 1;
}catch(Exception e){
System.out.println("catch代码块被执行");
return 2;
}finally{
System.out.println("finally代码块被执行");
return 3;
}
}
}
执行结果
try代码块被执行
catch代码块被执行
finally代码块被执行
method代码块中执行结果:3
分析
第一个案例中method执行结果为1,第二个案例中method代码块执行结果为2,第三个案例中method执行结果为3,这是因为finally代码块必须执行,因此在try或者catch中执行到return语句时,会暂时跳过该return语句(不是完全跳过该语句,后面会讲),去执行finally代码块,当执行完后在返回执行try或者是catch中的return语句,如果finally中有return语句,则不会执行try或者是catch中的return语句,相当于在执行try或者catch代码块中的return语句前程序已经结束了
//伪代码
if(finally有return)
return finally的返回值
else if(catch代码块或者try代码块有return)
return catch或者是try的返回值(取决于是从try还是catch中进入的finally)
案例四
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
try{
System.out.println("try代码块被执行");
return num();
}catch(Exception e){
System.out.println("catch代码块被执行");
return 2;
}finally{
System.out.println("finally代码块被执行");
}
}
public static int num(){
System.out.println("num方法被执行");
}
}
执行结果
try代码块被执行
num方法被执行
finally代码块被执行
method代码块中执行结果:4
案例五
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
try{
System.out.println("try代码块被执行");
int i = 1 / 0;
return 1;
}catch(Exception e){
System.out.println("catch代码块被执行");
return num();
}finally{
System.out.println("finally代码块被执行");
}
}
public static int num(){
System.out.println("num方法被执行");
}
}
执行结果
try代码块被执行
catch代码块被执行
num方法被执行
finally代码块被执行
method代码块中执行结果:4
分析
案例四和案例五说明了,当try或者catch中的return语句是返回一个函数的返回值时,会先执行该函数,获得返回值,再执行finally代码块
案例六
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
int index = 1;
try{
System.out.println("try代码块被执行");
return index;
}catch(Exception e){
System.out.println("catch代码块被执行");
return 3;
}finally{
System.out.println("finally代码块被执行");
index++;
System.out.println("finally中index=" + index);
}
}
}
执行结果
try代码块被执行
finally代码块被执行
finally中index=2
method代码块中执行结果:1
案例七
class Test{
public static void main(String[] args){
System.out.println("method代码块中执行结果:" + method);
}
public static int method(){
int index = 1;
try{
System.out.println("try代码块被执行");
int i = 1 / 0;
return index;
}catch(Exception e){
System.out.println("catch代码块被执行");
return index;
}finally{
System.out.println("finally代码块被执行");
index++;
System.out.println("finally中index=" + index);
}
}
}
执行结果
try代码块被执行
finally代码块被执行
catch代码块被执行
finally中index=2
method代码块中执行结果:1
分析
案例六和案例七说明了,当跳过try或者catch的返回值时,会保存该返回值,即使finally代码块中改变了该值,但是当返回执行跳过的return时,返回的是保存的值,不管finally怎样对该值进行修改

浙公网安备 33010602011771号