先看一段代码:
public class Test { public static int method(){ int a=0; try { int x = 1/0; a++; } catch (Exception e) { e.printStackTrace(); a++; return a; }finally{ a++; return a; } } public static void main(String[] args) { System.out.println(Test.method()); } }
输出结果:
java.lang.ArithmeticException: / by zero at test_try_catch.Test.method(Test.java:9) at test_try_catch.Test.main(Test.java:21) 2
代码中,catch里的return 被finally里的return覆盖了。