具体如下代码:
package com.example.demo.java;
import java.util.ArrayList;
import java.util.List;
public class FinallyTest {
private static int a =10;
private static List intList = new ArrayList();
public static void main(String[] args) {
System.out.println("finallyTest1:" +finallyTest1());//执行结果返回13
System.out.println("finallyTest2:" +finallyTest2());//执行结果返回12
System.out.println("finallyTest3:" +finallyTest3());//执行结果返回11
System.out.println("finallyTest4:" +finallyTest4().get(0));//执行结果返回20
}
public static int finallyTest1(){
try{
a = 11;
throw new Exception();
}catch (Exception e){
e.printStackTrace();
}finally {
a=13;
return a;
}
}
public static int finallyTest2(){
try{
a = 11;
return a;
}catch (Exception e){
e.printStackTrace();
}finally {
return a =12;
}
}
public static int finallyTest3(){
try{
a = 11;
return a;
}catch (Exception e){
e.printStackTrace();
}finally {
a =14;
}
return a;
}
public static List finallyTest4(){
try{
a = 11;
intList.add(a);
return intList;
}catch (Exception e){
e.printStackTrace();
}finally {
intList.set(0,20);
}
return intList;
}
}
结果如截图:



