uplifitng return value error reporting to Exception based error reporting In Framework Design guideline book there is a chapter about Exception and they talk about return-value-based error reporting and exception based error reporting and the fact that we in a O.O language like C# we should avoid return-value-based error reporting and use exceptions. With that in mind I was looking at our code that eight years ago was written in Visual Basic and last year with a automatic tool got converted to C#!

So here is a method I am looking at, I was wondering if the advice from that book applies to such a method and if yes, then what would be a better approach for rewriting this method? 

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;
    }

    if (oCaseStep.Update(oCaseStepIn) == UCommonIndep.gnUPDATE_SUCCESSFUL)
    {
        //*******************************
        //FYI - Insert code here to update any Key values that might have changed.
        result = UCommonIndep.gnUPDATE_SUCCESSFUL;
    }
    return result;
}
Yes, the refactoring "Replace error codes with exceptions" work perfectly here.
As a first step you need to create an exception type that suits your need, something like

public class UpdateException : Exception
{
    //your custom properties, fields etc.
}


and then use it in your method (an example below)

public void Update(CaseStep oCaseStepIn)
{
    //Update the master object with the passed in object

    if (Validate(oCaseStepIn) == UCommonIndep.gnVALIDATE_FAILED) 
        throw new UpdateException(); //pass in constructor useful messages

    CaseStep oCaseStep = get_ItemByObjectKey(oCaseStepIn.CopyOfObjectKey);
    if (oCaseStep == null)
        throw new UpdateException(); //pass in constructor useful messages


    if (oCaseStep.Update(oCaseStepIn) != UCommonIndep.gnUPDATE_SUCCESSFUL)
        throw new UpdateException(); //pass in constructor useful messages

    //*******************************
    //FYI - Insert code here to update any Key values that might have changed.
}


You can create different Exception subclasses for each type of error. It's even better.