一道关于java finally 、return 简单面试题
今天就简单的讲讲一道,你在做笔试题的时候可能会遇到的题目
一道关于java finally 、return 简单面试题。
你懂了吗?
首先,举出例子,然后看执行的顺序是什么样的,你就懂了哈
这段代码的运行结果是什么?很多人看来了会懵逼。
public class TestQA {
public static void main(String[] args) {
int add = add(1, 2);
System.out.println(add);
}
public static int add(int a, int b) {
try {
return a + b;
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("finally块");
}
return 0;
}
}
打断点看这段代码是怎么走的。
执行F5
现在你应该懂了结果为什么是 finally 块和3了,而且不是 0,或其他的