1. Unlike return codes, the checked exceptions force the programmer to deal with exceptional conditions, greatly enhancing reliability.
2. An unchecked exception is more appropriate unless the exceptional condition cannot be prevented by proper use of the API and the programmer using the API can take some useful action once confronted with the exception.
3. If the programmer using the API can do no better than the following, an unchecked exception would be more appropriate:
catch(TheCheckedException e) { throw new AssertionError(); // Can't happen! } //or catch(TheCheckedException e) { e.printStackTrace(); // Oh well, we lose. System.exit(1); }
One example of an exception that fails this test is CloneNotSupportedException. In practice, the catch block almost always has the character of an assertion failure. The checked nature of the exception provides no benefit to the programmer, but it requires effort and complicates programs.
4. One technique for turning a checked exception into an unchecked exception is to break the method that throws the exception into two methods, the first of which returns a boolean that indicates whether the second method can be invoked without exception condition:
if (obj.actionPermitted(args)) { obj.action(args); } else { // Handle exceptional condition ... }
If an object is to be accessed concurrently without external synchronization or it is subject to externally induced state transitions, this refactoring is inappropriate, as the object’s state may change between the invocations of actionPermitted and action. If a separate actionPermitted method would, of necessity, duplicate the work of the action method, the refactoring may be ruled out by performance concerns.
相关推荐
Item 71: Avoid unnecessary use of checked exceptions Item 72: Favor the use of standard exceptions Item 73: Throw exceptions appropriate to the abstraction Item 74: Document all exceptions thrown by ...
Item 16: Avoid Creating Unnecessary Objects 94 Item 17: Implement the Standard Dispose Pattern 98 Item 18: Distinguish Between Value Types and Reference Types 104 Item 19: Ensure That 0 Is a Valid ...
Use 0 (default) for small projects, use 1 for large projects (if ecc32 runs out of memory). 2)....Added: --el_DisableDebuggerPresent command-line option for compatibility with 3rd party debuggers ...
• Avoid unnecessary complexity, making the platform easy to learn and to use. • Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. • Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
Avoid unnecessary complexity, making the platform easy to learn and to use. ? Provide an implementation that is efficient in network bandwidth, memory use, and CPU overhead. ? Provide an ...
The app also does not need any unnecessary permissions. A simple Android browser based on webview. The base is "Ninja" (https://github.com/mthli/Ninja). The intention is to provide a simple and light...
How (and why) do you avoid unnecessary initialization, cyclic, and definitional dependencies? When (and how) should you use static and dynamic polymorphism together? How do you practice "safe" ...
Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and Predecrement Use of const Integer Types 64-bit Portability Preprocessor Macros 0 and ...
Discusses analysis from the start of the book, to avoid unnecessary discussion on real numbers beyond what is immediately needed. Includes simplified and meaningful proofs. Features Exercises and ...
Compute values lazily and avoid unnecessary memory allocations using generators Write concurrent code that doesn't feel like concurrent code by abstracting away boilerplate chores Leverage true ...
- Unnecessary focus changing was fixed - Calc panel: strings/numbers aggregation toggle was added - Data transfer: fetch size was added in export settings (may significantly improve performance) -...
当我们需要清理代码并删除那些不需要的代码碎片时,Unnecessary Code Detector(https://marketplace.eclipse.org/content/unnecessary-code-detector/metrics) 就可以完成这项工作。当你点击按钮该工具就会出现标志...
To avoid unnecessary distractions, the applications in this book use a text rather than a GUI interface; if you are programming for the PC under Windows use the Win32 Console Application project type....
Usually, getTableCellRendererComponent() will return the same component for every cell of a column, to avoid the unnecessary use of extra memory. But when the contents of a cell is itself a component...
- Corrected bugs which caused unnecessary exceptions when sending data at low baud rates with CheckAllSends = false. - Fixed bug in setting RTS state. - Deleted virtual function CommBase.OnRing...