try-catch-finally执行顺序

来源:互联网 发布:淘宝开店的具体步骤 编辑:程序博客网 时间:2024/06/06 00:08
public class Test {
public static void main(String[] args) {
int i = ma();
System.out.println("i="+i);
}
public static int ma(){
int i = 5;
try{
System.out.println("try"+i/0);
return i;
}catch (Exception e) {
System.out.println("catch"+i);
return i;


}finally{
++i;
System.out.println("finally"+i);
//return i;
}
}

}

说明: 

不出现异常情况: 

执行顺序: try  ----- finally

出现异常情况:

执行顺序:try -----catch ----finally

try中有返回语句,,没有异常:

执行顺序: try -----return ----finally------return

try中有返回语句,有异常:

执行 顺序: try----catch -----finally----return


执行过程,可以通过Debug进行调试,查看执行过程。

0 0
原创粉丝点击