关于try catch finally执行顺序的一点疑问
public class TryCatchOrder {public static void main(String[] args) {System.out.println(new TryCatchOrder().test());}static int i = 0;public int test() {try {System.out.println("抛出异常!");throw new Exception();} catch (Exception e) {System.out.println("进入catch块了!");return ++i;} finally {System.out.println("进入finally块了!");++i;}}}
?输出为:1
?
?
public class TryCatchOrder {public static void main(String[] args) {System.out.println(new TryCatchOrder().test());}static int i = 0;public int test() {try {System.out.println("抛出异常!");throw new Exception();} catch (Exception e) {System.out.println("进入catch块了!");return ++i;} finally {System.out.println("进入finally块了!");return ++i;}}}
?
输出为:2
?
经过调试,我发现,第一个例子里面的执行顺序是catch--finally--catch,第二个例子里面是catch--finally
还是有点没懂,为什么第一个例子的输出是 1。
?
?
?
?
public class TryCatchOrder {public static void main(String[] args) {System.out.println(new TryCatchOrder().test());}static int i = 0;public int test() {try {++i;throw new IllegalArgumentException("明明发生了异常,怎么没有失败啊。。。");} finally {System.out.println("进入finally块了!");return ++i;}}}