异常小tips
还有当一行代码发生异常的时候,下一行代码执行不到,但是此行的++、--(无论变量前的还是变量后的)运算会执行,只是赋值运算不会执行。
例如:
1 public class Test 2 { 3 public static void main(String[] args) { 4 int[] arr = new int[4]; 5 int i = 4,j = 0; 6 try{ 7 j = arr[i++]; 8 }finally{ 9 System.out.println(i); // 5 10 System.out.println(j); // 0 11 } 12 } 13 }
运行结果为:
5 0 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at Test.main(Test.java:7)
从结果可以证明:虽然给 j 赋值运算因异常没有执行,但是 i++ 运算了。

浙公网安备 33010602011771号