2012-04-20 10 views
0

フレームワークデザインガイドラインブックには、例外に関する章があり、戻り値ベースのエラー報告と例外ベースのエラー報告と、C#のようなOO言語戻り値ベースのエラー報告と例外の使用を避ける。そのことを念頭に置いて私は8年前にVisual Basicで書かれたコードを見ていて、昨年は自動ツールをC#に変換しました。例外へのリターン値エラーの報告

これは私が見ている方法ですが、その本のアドバイスがそのような方法に当てはまるのかどうか疑問に思っていました。もしそうなら、この方法を書き直す方がいいでしょうか?可能であれば特定の例外を投げてください。

public int Update(CaseStep oCaseStepIn) 
{ 
    int result = 0; 
    //Update the master object with the passed in object 

    result = UCommonIndep.gnUPDATE_FAILED; 
    if (Validate(oCaseStepIn) == UCommonIndep.gnVALIDATE_FAILED) 
    { 
     return result; 
    } 

    CaseStep oCaseStep = get_ItemByObjectKey(oCaseStepIn.CopyOfObjectKey); 
    if (oCaseStep == null) 
    { 
     return result; 
    } 

    return result; 
} 

答えて

1

可能であれば特定の例外を投げてください。この場合、の戻り値は必要ありません。

public void Update(CaseStep oCaseStepIn) 
{ 
    //Update the master object with the passed in object 
    if (Validate(oCaseStepIn) == UCommonIndep.gnVALIDATE_FAILED) 
     throw new ValidationFailedUpdateException(); 

    CaseStep oCaseStep = get_ItemByObjectKey(oCaseStepIn.CopyOfObjectKey); 
    if (oCaseStep == null) 
     throw new KeyObjectNotFoundUpdateException(); 

    if (oCaseStep.Update(oCaseStepIn) != UCommonIndep.gnUPDATE_SUCCESSFUL) 
     throw new UpdateFailedException(); 

    //******************************* 
    //FYI - Insert code here to update any Key values that might have changed. 
} 
  • UpdateFailedExceptionは例外に拡張し
  • ValidationFailedUpdateExceptionが
  • KeyObjectNotFoundUpdateExceptionがUpdateFailedExceptionを拡張UpdateFailedExceptionを拡張
0

あります(少なくとも)例外処理のように多くの意見がありコーダーですが、良いよう経験則から見ると、例外的な状況では例外が投げられるべきです。

更新に失敗しましたか?

関連する問題