看板 Ruby 關於我們 聯絡資訊
今天在Ruby Forum看到一篇討論串 探討The Ruby Way這本書對於else的解釋 原網址:http://www.ruby-forum.com/topic/90997 書:http://www.informit.com/store/product.aspx?isbn=0672328844 在書裡頭的節錄: In the event that error types are specified, it may be that an exception does not match any of these types. For that situation, we are allowed to use an else clause after all the rescue clauses. 意思是如果值出的異常我們都沒有救到的話(rescue),則會執行else clause 這個解釋我也曾在書上看過 然而實驗的證明 else clause只會在"沒有任何異常擲出"時才會執行 意思是下面這兩個寫法結果是一樣的: ---------- begin # Nothing Happens rescue else puts "Do something if no exceptoin was throwed." end ---------- begin # Nothing Happens rescue end puts "Do something if no exceptin was throwed." ---------- 問題: 1. 這兩種寫法既然結果一樣,但有沒有什麼理由能讓我們從中選擇一個比較好的寫法? 否則else感覺沒有什麼大用 2. 會有else行為的爭議,是因為Ruby版本的問題嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.235.115