1. try代码块中包含return语句,finally代码块中不包含return语句。
public class Test {
public static void main(String[] args) {
System.out.println(test());
}
static int test() {
int x=1;
try {
System.out.println("try语句块:"+x);
return x;
}catch(Exception e) {
x++;
System.out.println("catch: "+x);
}finally {
x++;
System.out.println("finally语句块:"+x);
}
return x;
}
}
执行结果:
因为try中带有return,会先执行return前的代码,然后暂时保存需要return的信息,再执行finally中的代码,最后通过return返回之前保存的信息。但是当return的类型不为基本类型时,结果会是怎样的呢?
public List<Integer> test() {
List<Integer> list= new ArrayList<Integer>();
int x=1;
try {
list.