看板 java 關於我們 聯絡資訊
剛剛看到前人寫的Code類似下面這樣的Function, public String greeting(boolean malicious) throws Exception{ try{ if(malicious){ throw new Exception("是在哈囉!"); } }catch(Exception ex){ throw ex; }finally{ return "Hello World!"; } } 這Function,怎麼看怎麼怪, 如果我呼叫 greeting(true) 會回我什麼結果? 事實上不管怎麼改 Exception 的型態,例如 public String greeting(boolean malicious) throws IOException{ try{ if(malicious){ throw new SQLException("是在哈囉!"); } }catch(Exception ex){ throw new IOException(ex.getMessage()); }finally{ return "Hello World!"; } } 永遠都會回傳Hello World! 後來找了一下stackoverflow,人家指到 https://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.2 原來裡面講了,意思大概如下: try - finally 述句首先執行 try 區塊,然後選擇 A.如果try塊的執行正常完成,則執行finally塊,然後選擇下列其一: a.如果 finally 區塊正常完成,則try述句正常完成 b.如果因為 S 原因完結,則try述句因S而完結(我的理解是例如finally中間因S出現ret urn或例外) B.如果try塊的執行因throw V而完結,然後選擇下列其一: a.如果執行時期V的Class被宣告在catch區塊,則第一個符合的catch區塊被執行,然後 選擇下列其一: …不管怎麼說,都會執行 finally 區塊 b.如果執行時期V的Class不在catch區塊內,則執行 finally 區塊,然後選擇下列其一 … 所以不管如何finally都會執行,符合印象中,finally一定會被執行的記憶, 所以rethrow Exception 根本是多餘的。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.138.125.98 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/java/M.1602831806.A.C1C.html
LZN: 不懂你想表達的意思, throw exception就是讓呼叫端得知有 10/16 15:44
LZN: exception發生,何來多餘一說? 10/16 15:45
Chikei: 他的意思是catch裡的rethrow是無效的,因為finally的 10/16 16:34
Chikei: return轉移流程的優先權比較高 10/16 16:34
ssccg: finally就優先啊,這寫法依常理來說就有問題 10/16 16:47
LZN: 了解, 只要在finally return, throw的exception就被忽略了.. 10/16 17:54
ssccg: JLS裡面寫到最重要的點原PO沒貼出來 10/16 17:55
ssccg: 在B.a.那項,如果catch因R而中途完結,執行finally,然後 10/16 18:00
ssccg: 1.finally正常完結,則整個try視為因R中途完結(rethrow) 10/16 18:01
ssccg: 2.如果finally因S中途完結,則整個try視為因S中途完結,R會 10/16 18:02
ssccg: 直接被丟棄(finally return,所以rethrow的結果R直接丟棄) 10/16 18:02
ssccg: (中途完結complete abruptly=break/continue/return/throw) 10/16 18:04
internetms52: 推,長知識 10/21 07:15
dream1124: 推 12/31 18:28