關於Java中try-catch-finally-return的執行順序
原文地址:http://qing0991.blog.51cto.com/1640542/1387200
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public static int NoException(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); return --i; } catch (Exception
e){ --i; System.out.println( "i
in catch - form try block is:" +i); return --i; } finally { System.out.println( "i
in finally - from try or catch block is:" +i); return --i; } } |
1
2
3
4
5
|
public static void main( String []
args) { System.out.println( "=============NoException==================" ); System.out.println(NoException()); System.out.println( "===============================" ); } |
1
2
3
4
5
|
=============NoException================== i in try block is : 10 i in finally -
from try or catch block is : 9 8 =============================== |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static int NoException1(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); return --i; } catch (Exception
e){ --i; System.out.println( "i
in catch - form try block is:" +i); return --i; } finally { System.out.println( "i
in finally - from try or catch block is:" +i); --i; System.out.println( "i
in finally block is:" +i); //return
--i; } } |
1
2
3
4
5
6
|
=============NoException1================== i in try block is : 10 i in finally -
from try or catch block is : 9 i in finally block is : 8 9 =============================== |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public static int WithException(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); i
= i/ 0 ; return --i; } catch (Exception
e){ System.out.println( "i
in catch - form try block is:" +i); --i; System.out.println( "i
in catch block is:" +i); return --i; } finally { System.out.println( "i
in finally - from try or catch block is--" +i); --i; System.out.println( "i
in finally block is--" +i); return --i; } } |
1
2
3
4
5
6
7
8
|
=============WithException================== i in try block is : 10 i in catch -
form try block is : 10 i in catch block is : 9 i in finally -
from try or catch block is -- 8 i in finally block is -- 7 6 =============================== |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public static int WithException1(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); i=i/ 0 ; return --i; } catch (Exception
e){ System.out.println( "i
in catch - form try block is:" +i); return --i; } finally { System.out.println( "i
in finally - from try or catch block is:" +i); --i; System.out.println( "i
in finally block is:" +i); //return
i; } } |
1
2
3
4
5
6
7
|
=============WithException1================== i in try block is : 10 i in catch -
form try block is : 10 i in finally -
from try or catch block is : 9 i in finally block is : 8 9 =============================== |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public static int WithException2(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); i=i/ 0 ; return --i; } catch (Exception
e){ System.out.println( "i
in catch - form try block is:" +i); int j
= i/ 0 ; return --i; } finally { System.out.println( "i
in finally - from try or catch block is:" +i); --i; --i; System.out.println( "i
in finally block is:" +i); return --i; } |
1
2
3
4
5
6
7
|
=============WithException2================== i in try
block is :10 i in catch
- form try block is :10 i in finally
- from try or catch
block is :10 i in finally
block is :8 7 =============================== |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
public static int WithException3(){ int i= 10 ; try { System.out.println( "i
in try block is:" +i); i=i/ 0 ; //return
--i; } catch (Exception
e){ System.out.println( "i
in catch - form try block is:" +i); //int
j = i/0; //return
--i; } finally { System.out.println( "i
in finally - from try or catch block is:" +i); --i; --i; System.out.println( "i
in finally block is:" +i); //return
--i; } return --i; } |
1
2
3
4
5
6
7
|
=============WithException3================== i in try block is : 10 i in catch -
form try block is : 10 i in finally -
from try or catch block is : 10 i in finally block is : 8 7 =============================== |
return語句並不是函式的最終出口,如果有finally語句,這在return之後還會執行finally(return的值會暫存在棧裡面,等待finally執行後再返回)
結論二:
finally裡面不建議放return語句,根據需要,return語句可以放在try和catch裡面和函式的最後。可行的做法有四:
(1)return語句只在函式最後出現一次。
(2)return語句僅在try和catch裡面都出現。
(3)return語句僅在try和函式的最後都出現。
(4)return語句僅在catch和函式的最後都出現。
注意,除此之外的其他做法都是不可行的,編譯器會報錯。
相關文章
- 關於 Promise 的執行順序Promise
- 關於defer執行順序問題
- 關於JAVA中順序IO的基本操作Java
- Java中如何保證執行緒順序執行Java執行緒
- java繼承關係下執行順序Java繼承
- Java for迴圈中語句執行的順序Java
- Java類的基本執行順序Java
- mysql 中sql語句關鍵字的書寫順序與執行順序MySql
- 關於describe和test執行順序的翻譯
- 關於java的初始化順序Java
- Java中,類與類,類中的程式碼執行順序Java
- # 關於select關鍵字語句定義順序# 關於select關鍵字語句執行順序
- java執行緒優先順序Java執行緒
- Java之執行緒的優先順序Java執行緒
- JavaScript的執行順序JavaScript
- Sql執行順序SQL
- sql中的or與and的執行順序問題SQL
- java類內部程式碼執行順序Java
- JavaScript執行順序分析JavaScript
- 任務執行順序
- for語句執行順序
- laravel Event執行順序Laravel
- mySQL 執行語句執行順序MySql
- C#中Page執行順序:OnPreInit()、OnInit()……C#
- Java含有return 的try catch finally的執行順序Java
- Spring Aop的執行順序Spring
- SQL 語句的執行順序SQL
- CSS規則的執行順序CSS
- SQL 執行順序 你懂的SQL
- pipeline的執行順序
- mysql 語句的執行順序MySql
- Js中async/await的執行順序詳解JSAI
- 關於RocketMQ的順序訊息MQ
- java中異常處理finally和return語句的執行順序Java
- 關於MQ的幾件小事(五)如何保證訊息按順序執行MQ
- 關於觸發器在行級和語句級的執行順序問題觸發器
- JS中的async/await的執行順序詳解JSAI
- SQL中rownum和order by的執行順序的問題SQL