0

这是示例代码片

public OperationResult beforeEverything(BDDObject savingObject) {

  String checkAssetRole = doCheckAssetRole(savingObject);

  if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) {
    return new OperationResult(new OperationExecutionError("SIP-37006",
                               new String[] {"Duplicate asset roles have been defined: " + checkAssetRole},
                               getLocalizationGate()));
  }
  ArrayList<String> warnings = new ArrayList<String>();
  boolean showWarning = false;

  if (checkAssetRole != null && checkAssetRole.equals("MissingAssetRole")) {
    mLogger.debug("Warning  of Asset role");
    warnings.add(new String(
                "Asset role is missing. Do you want to save the record?"));
    showWarning = true;
  }
  return OperationResult.OK;
}

问题是doCheckAssetRole方法返回null。那么我该如何在beforeEverything()方法中处理它。是否需要进行一些异常处理?如果是这样,如何?

4

4 回答 4

1

放置一个null检查,然后抛出一个RuntimeExceptionwith message

 if(checkAssetRole==null)
      throw new RuntimeException ("AssetRole value is NULL.");

你的代码看起来像这样。

String checkAssetRole = doCheckAssetRole(savingObject);

if(checkAssetRole==null)
      throw new RuntimeException ("AssetRole value is NULL.");

if (checkAssetRole != null && !checkAssetRole.equals("MissingAssetRole")) {
return new OperationResult(new OperationExecutionError("SIP-37006",
                           new String[] {"Duplicate asset roles have been defined: " + checkAssetRole},
                           getLocalizationGate()));
}
于 2012-07-26T09:50:09.453 回答
1

如果 doCheckAssetRole 为空,这取决于您想要的是您的业务逻辑:

  • 它对应于一个有效的业务用例,所以你有一个对应的 OperationResult 对象来返回这个案例,所以你会做类似的事情

    if (checkAssetRole == null)
      return new NoAssetRoleOperationResult();
    

    调用代码将处理这种结果。

  • 它永远不应该发生,除非用户犯了错误,然后你抛出一个检查异常。

    if (checkAssetRole == null)
      throw new NoAssetRoleException(yourMessage);
    

    然后你做你的方法声明throws NoAssetRoleExeption。然后调用代码负责将此错误转发回用户。

  • 它由于环境错误(远程服务器已关闭)而失败,您抛出一个未经检查的异常,例如RuntimeException,顶级代码将捕获它以指示存在环境故障。

  • 它因为开发错误而失败(理论上这段代码不应该返回 null),然后你断言:

    assert checkAssetRole != null : "assetRole should not be null"
    

选择你的案例:)

于 2012-07-26T09:57:33.870 回答
0

这取决于返回 null 时您想要做什么。如果你想在 doCheckAssetRole 返回 null 时返回一个值 NotOK,那么你可以使用这个片段。

public OperationResult beforeEverything(BDDObject savingObject) {

  String checkAssetRole = doCheckAssetRole(savingObject);

  if (checkAssetRole == null) {
    return OperationResult.NotOK; // Or something else that indicates to the calling method what happened.
  else {
    if (!checkAssetRole.equals("MissingAssetRole")) {
      return new OperationResult(new OperationExecutionError("SIP-37006",
                                 new String[] {"Duplicate asset roles have been defined: " + checkAssetRole},
                                 getLocalizationGate()));
    }
    ArrayList<String> warnings = new ArrayList<String>();
    boolean showWarning = false;

    if (checkAssetRole.equals("MissingAssetRole")) {
      mLogger.debug("Warning  of Asset role");
      warnings.add(new String(
                   "Asset role is missing. Do you want to save the record?"));
      showWarning = true;
    }
    return OperationResult.OK;
  }
}
于 2012-07-26T09:56:30.293 回答
0

从方法返回null是正常的做法。调用者需要检查它。在您的情况下,您可以进行检查并采取相应措施。如果你想抛出异常,那么你可以这样做。

   public OperationResult beforeEverything(BDDObject savingObject) {

     String checkAssetRole = doCheckAssetRole(savingObject);

     if (checkAssetRole == null) {
         // DO SOMETHING HERE. eg. throw new RuntimeException("Asset Role should not be null");
         //   or simply return null;

     }
于 2012-07-26T09:53:00.243 回答