看板 java 關於我們 聯絡資訊
出個懶人包… 因為討論的是 Java 的物,所以 "人" 的部分就不重要了 1. 問: 為什麼 Thread.Sleep() 要用 try-catch 來坦? 2. 某人開大絕放 LMGTFY 出來咬人; 點出這個 link http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html 3. 有人佛心來著 po 了一段把 Thread.Sleep() 用 try-catch 坦起來且一勞永逸的辦法 4. 某人出來碎碎唸;主張 InterruptedException 背後的案情並不單純 http://www.ibm.com/developerworks/java/library/j-jtp05236.html 5. 某人繼續碎碎唸;主張所有 checked exception 背後的案情都不單純,難以一言盡之 http://tinyurl.com/yewgxls 6. 有人提出一個很有趣的說法;主張一言得以盡之;此一言為: sleep 被意外中斷很容易修復,所以被包在 checked exception 7. 某人請有人對 "很容易修復 --所以--> 被包在 checked exception" 這背後的邏輯關係再作解釋 反過來說,也就是討論開始走向 checked exception 與 unchecked exception 的界限在哪裡? 8. 某人與有人就跑去以信件討論到底何謂 "很容易修復" "修復" 的定義為何? "容易" 的定義又為何? 9. 最後,某人把 http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html 再仔細看了一次,才發現,其實 Java 規格書很簡單的就把事情講清楚了; (易言之,某人之前的碎碎唸像是在重新發明輪子一樣…) 11.2 Compile-Time Checking of Exceptions A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.6) or constructor (§8.8.5) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled. 11.2.5 Why Runtime Exceptions are Not Checked The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer. Requiring such exception classes to be declared would simply be an irritation to programmers. For example, certain code might implement a circular data structure that, by construction, can never involve null references; the programmer can then be certain that a NullPointerException cannot occur, but it would be difficult for a compiler to prove it. The theorem-proving technology that is needed to establish such global properties of data structures is beyond the scope of this specification. 也就是說 1. 之所以在 compile time 來 check 某些 exception 是否有被處理 是為了減少在程式裡沒有被處理的 exception 的數量 --> 這就是所謂 checked exception 2. 之所以另一些 exception 沒有在 compile time 被 check 是因為 1. 對 compiler 來說,這一類 exception 太難 check 了 (註1) 讓 programmer 自己去避免吧 2. 在 Java 設計者的主觀認定裡 就算強制在 compile time 來 check 這類 exception 對程式的正確度也沒什麼幫助 --> 此為 unchecked exception 或 runtime exception Java 規格書萬歲!! 註1: static analysis 一直在進步 Java 上早已經有各式各樣研究的實作, C# 就比較少,只有 Microsoft Research 自己在搞 但在 .Net 4.0 裡 contract-based programming 將昇為第一等公民, 且加入了對應的 static analysis 的工具 .Net 4.0 Beta2 快出來了,有興趣可以嘗試看看 C# 與 static analysis -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 65.87.177.87
PsMonkey:居然能等得到懶人包... Java 版救星阿... (y) 10/08 11:03
H45:阿...私下信件還在進行中耶 XD 10/08 11:20
kene:那我們可以等待懶人包 SP1 嗎? XD 10/08 12:06
這幾天打的中文的量讓我已經沒力了… XD 要請 H45 接手了
PsMonkey:私通是不道德的 [怒指] 10/08 15:21
我也是不得已啊;這個版空白的地方太小,以致於我無法寫下所有的論證… XD
qrtt1:effective java 有章專講 exception :) 10/08 23:00
一直沒機會好好把這本書看完,後來就跑去搞C#了 XD ※ 編輯: AmosYang 來自: 65.87.177.87 (10/09 08:18)