Best way to handle repetitive error code or return value I have a wrapper which interacts with a third party component. In Each function i am performing the operation if it the component is initialized. 

I have other function to check the errorCode and component initialization check. 

Is this right way to do this? I am not happy with the readability of this code. Help me improving this code. 

Thanks in advance.

Void Operation1 (parameter someInput)
{   
    int errorCode= 0;
    if (Initialized)
    {  
        errorCode = ThirdPartyComponent.Operation1(someInput);
    }

     CheckErrorCodeAndShowError ("Operation1 with someInput failed", returnValue);    
}

Void Operation2 (parameter someInput)
{   
    int errorCode= 0;
    if (Initialized)
    {  
        errorCode = ThirdPartyComponent.Operation2(someInput);
    }

    CheckErrorCodeAndShowError ("Operation2 with someInput failed", returnValue);
}

Void CheckErrorCodeAndShowError (String errorMessage, int errorCode)
{   
   if (!Initialized)
   {
       //Show not initialized error
   }

   if (errorCode == -1)
   {
      //Show Operation failed error (errorMessage)
   }
   else if (errorCode == 0)
   {
      //Show operation success message etc
   }
   //other error code check
}
Okay, well here is what I would probably do in this situation. If the error codes you are getting are consistent this will make this type of solution even better (consistent as in "FileNotFound" error code is always -42 or something like that).

I would add a private method that throws an "Uninitialized" exception if the object has not been initialized. There is no need to be checking that twice for each function call.

private void ValidateInitialized()
{
    throw new LibraryUninitializedException(); // Just create this exception class
}


Then I would create a helper method that takes a Func&lt;int&gt; that you can call from each of the wrapper methods. The helper method will handle the error code stuff for you. It would look something like this (note: I have not compiled this, so I may have some minor syntax errors).

private void DoThirdPartyOperation(Func&lt;int&gt; operation, string message = "")
{
     ValidateInitialized();
     switch(operation())
     {
         case 0:
             // No error - do nothing
             break;
         case someErrorCode:
             throw new SomeErrorException(message);
             break;
         case someOtherErrorCode:
             throw new SomeOtherErrorException(message);
             break;

         default:
             // Choose either to throw some sort of general exception and include
             // the error code in it OR throw an UnrecognizedErrorException
     }
}

public void Operation1(someParameter)
{
    DoThirdPartyOperation(() =&gt; { ThirdParty.Operation1(someParameter); });
}

public void Operation2(someParameter)
{
    DoThirdPartyOperation(() =&gt; { ThirdParty.Operation2(someParameter); },
        "Failed performing Operation2");
}