java异常处理中的细节

首先看一段代码

 1 public class Test{  
 2     public static String output="";  
 3     public static void foo(int i){  
 4         try {  
 5             if(i==1){  
 6                 throw new Exception();  
 7             }  
 8             output +="1";  
 9         } catch(Exception e){  
10             output+="2";  
11             return;  
12         } finally{  
13             output+="3";  
14         }  
15         output+="4";  
16     }  
17     public static void main(String args[]){  
18         foo(0);  
19         foo(1);  
20         System.out.println(output);   
21     }  
22 }  
// 输出结果:13423
// 如果被调用的方法中用throw来抛出一个异常对象,但是该方法并没有处理该异常,
// 则在该方法中执行完finally语句块后,不会再执行finally之后的语句,而直接返回到
// 方法调用处,将异常交由调用该方法的方法处理,如果不处理将会checked exception;
// 如果被调用的方法中抛出的异常对象被catch处理了,则finally之后/的语句可以正常执行

  

纸上得来终觉浅 绝知此事要躬行
原文地址:https://www.cnblogs.com/lslk89/p/6898450.html