Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向异常报告提升返回值错误_C#_Error Handling - Fatal编程技术网

C# 向异常报告提升返回值错误

C# 向异常报告提升返回值错误,c#,error-handling,C#,Error Handling,在《框架设计指南》一书中有一章是关于异常的,他们讨论了基于返回值的错误报告和基于异常的错误报告,以及我们使用C#之类的O.O语言应该避免基于返回值的错误报告和使用异常这一事实。考虑到这一点,我在看我们的代码,八年前是用Visual Basic编写的,去年用一个自动工具转换成了C 这是我正在研究的一种方法,我想知道这本书的建议是否适用于这种方法,如果适用,那么重写这种方法的更好方法是什么 public int Update(CaseStep oCaseStepIn) { int resul

在《框架设计指南》一书中有一章是关于异常的,他们讨论了基于返回值的错误报告和基于异常的错误报告,以及我们使用C#之类的O.O语言应该避免基于返回值的错误报告和使用异常这一事实。考虑到这一点,我在看我们的代码,八年前是用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;
}

可能时抛出特定异常。然后,在这种情况下,您不需要返回值

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.
}
  • UpdateFileDexception扩展了异常
  • ValidationFailedUpdateException扩展了UpdateFailedException
  • KeyObjectNotFoundUpdateException扩展了UpdateFailedException
关于异常处理的意见(至少)和编码人员的意见一样多,但一个好的经验法则是,异常应该在异常情况下抛出

那么,更新失败是否属于例外情况