`
leonzhx
  • 浏览: 768048 次
  • 性别: 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# (Covers C# 4.0) Mar 2010

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

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

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

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

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

    Introduction to Analysis【Arthur P. Mattuck】

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

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

    dbeaver v6.0.2

    - 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

    当我们需要清理代码并删除那些不需要的代码碎片时,Unnecessary Code Detector(https://marketplace.eclipse.org/content/unnecessary-code-detector/metrics) 就可以完成这项工作。当你点击按钮该工具就会出现标志...

    The Pocket Guide To TCP/IP Sockets (C Version)

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

    使用 AbstractTableModel 构建Table 在表格中添加JButton按钮

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

Global site tag (gtag.js) - Google Analytics