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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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执行顺序的主要内容,如果未能解决你的问题,请参考以下文章

异常处理 try...catch...finally 执行顺序, 以及对返回值得影响

关于try catch finally的执行顺序解释

Java try/catch/finally内部执行顺序&外部语句何种情况下执行

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

try catch finally 中 returne的执行顺序

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