作者AmosYang (LetMeGoogleThatForYou)
看板java
标题Re: [J2SE] 请问delay的方法
时间Thu Oct 8 10:49:36 2009
出个懒人包…
因为讨论的是 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
1F:推 PsMonkey:居然能等得到懒人包... Java 版救星阿... (y) 10/08 11:03
2F:推 H45:阿...私下信件还在进行中耶 XD 10/08 11:20
3F:推 kene:那我们可以等待懒人包 SP1 吗? XD 10/08 12:06
这几天打的中文的量让我已经没力了… XD 要请 H45 接手了
4F:推 PsMonkey:私通是不道德的 [怒指] 10/08 15:21
我也是不得已啊;这个版空白的地方太小,以致於我无法写下所有的论证… XD
5F:推 qrtt1:effective java 有章专讲 exception :) 10/08 23:00
一直没机会好好把这本书看完,後来就跑去搞C#了 XD
※ 编辑: AmosYang 来自: 65.87.177.87 (10/09 08:18)