`
leonzhx
  • 浏览: 796712 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Item 59: Avoid unnecessary use of checked exceptions

阅读更多

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.

分享到:
评论

相关推荐

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    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 ...

    Effective C#

    **Item 16: Avoid Creating Unnecessary Objects** - **Impact:** Reducing object creation can improve performance by reducing memory pressure. - **Approach:** Reuse objects when possible and avoid ...

    The Elements of Style

    - 示例:Avoid unnecessary repetition.(避免不必要的重复。) - 作用:精简语言。 14. **避免连续使用松散句子**: - 示例:Avoid using loose sentences one after another. - 作用:提高文章质量。 15. **...

    JavaScript in 10 Minutes

    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...

    EurekaLog_7.5.0.0_Enterprise

    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 ...

    hui bian software code style guidelines

    - Optimize loops and avoid unnecessary computations. - Use profiling tools to identify bottlenecks. - **Security:** - Sanitize user input to prevent injection attacks. - Use secure coding ...

    ICE 3.3.1 DEMO

    • 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 ...

    Ice-3.3.1-ThirdParty-VC60

    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 ...

    Ice-3.3.1-VC80.part2

    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 ...

    Ice-3.3.1-VC80.part3

    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 ...

    Ice-3.3.1-VC80.part1

    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 ...

    Ice-3.3.1-VC60.part2

    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 ...

    Ice-3.3.1-VC60.part1

    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 ...

    Android代码-FOSS Browser

    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...

    Efficient MIDP Programming

    - **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...

    Addison.Wesley.C.Plus.Plus.Coding.Standards.101.Rules.Guidelines.and.Best.Practices.Oct.2004.eBoo.chm

    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" ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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 ...

    JavaScript Concurrency pdf 无水印 0分

    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 ...

Global site tag (gtag.js) - Google Analytics