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** - **Impact:** Reducing object creation can improve performance by reducing memory pressure. - **Approach:** Reuse objects when possible and avoid ...
- 示例:Avoid unnecessary repetition.(避免不必要的重复。) - 作用:精简语言。 14. **避免连续使用松散句子**: - 示例:Avoid using loose sentences one after another. - 作用:提高文章质量。 15. **...
1. **Why `new` is Awful**: Discusses the drawbacks of using the `new` operator, such as the creation of unnecessary objects and the lack of flexibility. 2. **Why `new` isn't Quite so Awful**: Explains...
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" ...
- **Reducing Screen Refresh Rates**: Reducing the frequency of screen refreshes can improve performance by reducing unnecessary drawing operations. ###### 2.1.3 Garbage Collection Garbage collection...
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 ...
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 ...