You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Item 69: Use exceptions only for exceptional conditions(仅在确有异常条件下使用异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md)
11
-
-[Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors(对可恢复情况使用 checked 异常,对编程错误使用运行时异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md)
12
-
-[Item 71: Avoid unnecessary use of checked exceptions(避免不必要地使用 checked 异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md)
13
-
-[Item 72: Favor the use of standard exceptions(鼓励复用标准异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-72-Favor-the-use-of-standard-exceptions.md)
14
-
-[Item 73: Throw exceptions appropriate to the abstraction(抛出能用抽象解释的异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-73-Throw-exceptions-appropriate-to-the-abstraction.md)
15
-
-[Item 74: Document all exceptions thrown by each method(为每个方法记录会抛出的所有异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-74-Document-all-exceptions-thrown-by-each-method.md)
16
-
-[Item 75: Include failure capture information in detail messages(异常详细消息中应包含捕获失败的信息)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-75-Include-failure-capture-information-in-detail-messages.md)
17
-
-[Item 76: Strive for failure atomicity(尽力保证故障原子性)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-76-Strive-for-failure-atomicity.md)
-[Item 69: Use exceptions only for exceptional conditions(仅在确有异常条件下使用异常)](/Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md)
11
+
-[Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors(对可恢复情况使用 checked 异常,对编程错误使用运行时异常)](/Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md)
12
+
-[Item 71: Avoid unnecessary use of checked exceptions(避免不必要地使用 checked 异常)](/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md)
13
+
-[Item 72: Favor the use of standard exceptions(鼓励复用标准异常)](/Chapter-10/Chapter-10-Item-72-Favor-the-use-of-standard-exceptions.md)
14
+
-[Item 73: Throw exceptions appropriate to the abstraction(抛出能用抽象解释的异常)](/Chapter-10/Chapter-10-Item-73-Throw-exceptions-appropriate-to-the-abstraction.md)
15
+
-[Item 74: Document all exceptions thrown by each method(为每个方法记录会抛出的所有异常)](/Chapter-10/Chapter-10-Item-74-Document-all-exceptions-thrown-by-each-method.md)
16
+
-[Item 75: Include failure capture information in detail messages(异常详细消息中应包含捕获失败的信息)](/Chapter-10/Chapter-10-Item-75-Include-failure-capture-information-in-detail-messages.md)
17
+
-[Item 76: Strive for failure atomicity(尽力保证故障原子性)](/Chapter-10/Chapter-10-Item-76-Strive-for-failure-atomicity.md)
Copy file name to clipboardexpand all lines: Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ try {
19
19
20
20
What does this code do? It’s not at all obvious from inspection, and that’s reason enough not to use it (Item 67). It turns out to be a horribly ill-conceived idiom for looping through the elements of an array. The infinite loop terminates by throwing, catching, and ignoring an ArrayIndexOutOfBoundsException when it attempts to access the first array element outside the bounds of the array. It’s supposed to be equivalent to the standard idiom for looping through an array, which is instantly recognizable to any Java programmer:
@@ -88,7 +88,7 @@ This should look very familiar after the array iteration example that began this
88
88
89
89
An alternative to providing a separate state-testing method is to have the statedependent method return an empty optional (Item 55) or a distinguished value such as null if it cannot perform the desired computation.
Here are some guidelines to help you choose between a state-testing method and an optional or distinguished return value. If an object is to be accessed concurrently without external synchronization or is subject to externally induced state transitions, you must use an optional or distinguished return value, as the object’s state could change in the interval between the invocation of a state-testing method and its state-dependent method. Performance concerns may dictate that an optional or distinguished return value be used if a separate statetesting method would duplicate the work of the state-dependent method. All other things being equal, a state-testing method is mildly preferable to a distinguished return value. It offers slightly better readability, and incorrect use may be easier to detect: if you forget to call a state-testing method, the statedependent method will throw an exception, making the bug obvious; if you forget to check for a distinguished return value, the bug may be subtle. This is not an issue for optional return values.
94
94
@@ -99,5 +99,5 @@ In summary, exceptions are designed for exceptional conditions. Don’t use them
**[Back to contents of the chapter(返回章节目录)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Introduction.md)**
103
-
-**Next Item(下一条目):[Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors(对可恢复情况使用 checked 异常,对编程错误使用运行时异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md)**
102
+
**[Back to contents of the chapter(返回章节目录)](/Chapter-10/Chapter-10-Introduction.md)**
103
+
-**Next Item(下一条目):[Item 70: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors(对可恢复情况使用 checked 异常,对编程错误使用运行时异常)](/Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md)**
Copy file name to clipboardexpand all lines: Chapter-10/Chapter-10-Item-70-Use-checked-exceptions-for-recoverable-conditions-and-runtime-exceptions-for-programming-errors.md
+7-7
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ The cardinal rule in deciding whether to use a checked or an unchecked exception
12
12
13
13
By confronting the user with a checked exception, the API designer presents a mandate to recover from the condition. The user can disregard the mandate by catching the exception and ignoring it, but this is usually a bad idea (Item 77).
There are two kinds of unchecked throwables: runtime exceptions and errors. They are identical in their behavior: both are throwables that needn’t, and generally shouldn’t, be caught. If a program throws an unchecked exception or an error, it is generally the case that recovery is impossible and continued execution would do more harm than good. If a program does not catch such a throwable, it will cause the current thread to halt with an appropriate error message.
@@ -25,7 +25,7 @@ There are two kinds of unchecked throwables: runtime exceptions and errors. They
25
25
26
26
One problem with this advice is that it is not always clear whether you’re dealing with a recoverable conditions or a programming error. For example, consider the case of resource exhaustion, which can be caused by a programming error such as allocating an unreasonably large array, or by a genuine shortage of resources. If resource exhaustion is caused by a temporary shortage or by temporarily heightened demand, the condition may well be recoverable. It is a matter of judgment on the part of the API designer whether a given instance of resource exhaustion is likely to allow for recovery. If you believe a condition is likely to allow for recovery, use a checked exception; if not, use a runtime exception. If it isn’t clear whether recovery is possible, you’re probably better off using an unchecked exception, for reasons discussed in Item 71.
27
27
28
-
这个建议存在的问题是,并不总能清楚是在处理可恢复的条件还是编程错误。例如,考虑资源耗尽的情况,这可能是由编程错误(如分配一个不合理的大数组)或真正的资源短缺造成的。如果资源枯竭是由于暂时短缺或暂时需求增加造成的,这种情况很可能是可以恢复的。对于 API 设计人员来说,判断给定的资源耗尽实例是否允许恢复是一个问题。如果你认为某个条件可能允许恢复,请使用 checked 异常;如果没有,则使用运行时异常。如果不清楚是否可以恢复,最好使用 unchecked 异常,原因将在 [Item-71](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md) 中讨论。
28
+
这个建议存在的问题是,并不总能清楚是在处理可恢复的条件还是编程错误。例如,考虑资源耗尽的情况,这可能是由编程错误(如分配一个不合理的大数组)或真正的资源短缺造成的。如果资源枯竭是由于暂时短缺或暂时需求增加造成的,这种情况很可能是可以恢复的。对于 API 设计人员来说,判断给定的资源耗尽实例是否允许恢复是一个问题。如果你认为某个条件可能允许恢复,请使用 checked 异常;如果没有,则使用运行时异常。如果不清楚是否可以恢复,最好使用 unchecked 异常,原因将在 [Item-71](/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md) 中讨论。
29
29
30
30
While the Java Language Specification does not require it, there is a strong convention that errors are reserved for use by the JVM to indicate resource deficiencies, invariant failures, or other conditions that make it impossible to continue execution. Given the almost universal acceptance of this convention, it’s best not to implement any new Error subclasses. Therefore, **all of the unchecked throwables you implement should subclass RuntimeException** (directly or indirectly). Not only shouldn’t you define Error subclasses, but with the exception of AssertionError, you shouldn’t throw them either.
31
31
@@ -37,17 +37,17 @@ It is possible to define a throwable that is not a subclass of Exception, Runtim
37
37
38
38
API designers often forget that exceptions are full-fledged objects on which arbitrary methods can be defined. The primary use of such methods is to provide code that catches the exception with additional information concerning the condition that caused the exception to be thrown. In the absence of such methods, programmers have been known to parse the string representation of an exception to ferret out additional information. This is extremely bad practice (Item 12). Throwable classes seldom specify the details of their string representations, so string representations can differ from implementation to implementation and release to release. Therefore, code that parses the string representation of an exception is likely to be nonportable and fragile.
39
39
40
-
API 设计人员常常忘记异常是成熟对象,可以为其定义任意方法。此类方法的主要用途是提供捕获异常的代码,并提供有关引发异常的附加信息。如果缺乏此类方法,程序员需要自行解析异常的字符串表示以获取更多信息。这是极坏的做法([Item-12](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-3/Chapter-3-Item-12-Always-override-toString.md))。这种类很少指定其字符串表示的细节,因此字符串表示可能因实现而异,也可能因版本而异。因此,解析异常的字符串表示形式的代码可能是不可移植且脆弱的。
40
+
API 设计人员常常忘记异常是成熟对象,可以为其定义任意方法。此类方法的主要用途是提供捕获异常的代码,并提供有关引发异常的附加信息。如果缺乏此类方法,程序员需要自行解析异常的字符串表示以获取更多信息。这是极坏的做法([Item-12](/Chapter-3/Chapter-3-Item-12-Always-override-toString.md))。这种类很少指定其字符串表示的细节,因此字符串表示可能因实现而异,也可能因版本而异。因此,解析异常的字符串表示形式的代码可能是不可移植且脆弱的。
41
41
42
42
Because checked exceptions generally indicate recoverable conditions, it’s especially important for them to provide methods that furnish information to help the caller recover from the exceptional condition. For example, suppose a checked exception is thrown when an attempt to make a purchase with a gift card fails due to insufficient funds. The exception should provide an accessor method to query the amount of the shortfall. This will enable the caller to relay the amount to the shopper. See Item 75 for more on this topic.
To summarize, throw checked exceptions for recoverable conditions and unchecked exceptions for programming errors. When in doubt, throw unchecked exceptions. Don’t define any throwables that are neither checked exceptions nor runtime exceptions. Provide methods on your checked exceptions to aid in recovery.
**[Back to contents of the chapter(返回章节目录)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Introduction.md)**
52
-
-**Previous Item(上一条目):[Item 69: Use exceptions only for exceptional conditions(仅在确有异常条件下使用异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md)**
53
-
-**Next Item(下一条目):[Item 71: Avoid unnecessary use of checked exceptions(避免不必要地使用 checked 异常)](https://github.com/clxering/Effective-Java-3rd-edition-Chinese-English-bilingual/blob/master/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md)**
51
+
**[Back to contents of the chapter(返回章节目录)](/Chapter-10/Chapter-10-Introduction.md)**
52
+
-**Previous Item(上一条目):[Item 69: Use exceptions only for exceptional conditions(仅在确有异常条件下使用异常)](/Chapter-10/Chapter-10-Item-69-Use-exceptions-only-for-exceptional-conditions.md)**
53
+
-**Next Item(下一条目):[Item 71: Avoid unnecessary use of checked exceptions(避免不必要地使用 checked 异常)](/Chapter-10/Chapter-10-Item-71-Avoid-unnecessary-use-of-checked-exceptions.md)**
0 commit comments