首页 > 代码库 > try...catch...finally执行顺序

try...catch...finally执行顺序

 1 package test;
 2 
 3 public class TestDemo {
 4     
 5     public static String output = "";
 6     
 7     public static void main(String[] args) {
 8         foo(0);
 9         foo(1);
10         System.out.println(output);
11     }
12 
13     private static void foo(int i) {
14         try {
15             if(i == 1){
16                 throw new Exception();
17             }
18         } catch (Exception e) {
19             output += "2";
20             return;
21         } finally{
22             output += "3";
23         }
24         output += "4";
25         
26         
27     }
28 
29 }

输出的结果

3423

 

try...catch...finally执行顺序