zoukankan      html  css  js  c++  java
  • Effective Java 59 Avoid unnecessary use of checked exceptions

    The burden is justified if 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.

    ask yourself how the programmer will handle the exception.

       

    Is this the best that can be done?

    } catch(TheCheckedException e) {

    throw new AssertionError(); // Can't happen!

    }

    How about this?

    } catch(TheCheckedException e) {

    e.printStackTrace(); // Oh well, we lose.

    System.exit(1);

    }

       

    Principle

    1. If the programmer using the API can do no better an unchecked exception would be more appropriate. The checked nature of the exception provides no benefit to the programmer, but it requires effort and complicates programs.
    2. 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 exception would be thrown.
       

    // Invocation with checked exception

    try {

    obj.action(args);

    } catch(TheCheckedException e) {

    // Handle exceptional condition

    ...

    }

       

    to this:

    // Invocation with state-testing method and unchecked exception

    if (obj.actionPermitted(args)) {

    obj.action(args);

    } else {

    // Handle exceptional condition

    ...

    }

       

    If you suspect that the simple calling sequence will be the norm, then this API refactoring may be appropriate. The API resulting from this refactoring is essentially identical to the state-testing method API in Item 57 and the same caveats apply: 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.

  • 相关阅读:
    Red Hat Enterprise Linux 7.2下使用RPM包安装SQL Server vNext
    VS2015解决方案资源管理器空白,不显示内容
    ArcEngine调用FeatureToLine工具传参问题
    ArcEngine调用GP里的Merge工具传参问题
    ArcGIS GP服务的发布及调用
    利用 Chrome 原生工具进行网页长截图
    关于ueditor与arcgis js api同用会报错的问题
    关于ueditor使用说明
    bootstraptable为行中的按钮添加事件
    关于html与body的高度问题
  • 原文地址:https://www.cnblogs.com/haokaibo/p/avoid-unnecessary-use-of-checked-exceptions.html
Copyright © 2011-2022 走看看