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