Exception handling in everymethod instead throwing and WrappedResult as return type












0















Recently I have started building new application, this is a .Net Core based MVC and Webapi application.



One of my senior came-up with Idea of WrappedResult as return type of each and every method. so that we need not to throw exception. Following is the structure of



public class WrappedResult<T>
{
public T Result { get; set; }
public bool IsSuccess
{
get
{
return !string.IsNullOrWhiteSpace(SuccessMessage);
}
}
public string SuccessMessage { get; set; }
public bool HasError
{
get
{
return Errors.Count > 0;
}
}

public List<RectoError> Errors { get; set; } = new List<RectoError>();

public void AddError(ErrorCode errorCode = ErrorCode.None, string errorMessage = null)
{
var rectoError = new RectoError()
{
ErrorCode = errorCode,
ErrorMessage = errorMessage,
};
Errors.Add(rectoError);
}

public void AddError(RectoError rectoError)
{
Errors.Add(rectoError);
}

public Task<WrappedResult<T>> ToTask()
{
return Task.Factory.StartNew(() => {
return this;
});
}
}

public class RectoError
{
public ErrorCode ErrorCode { get; set; }
public Exception Exception { get; set; }
public string ErrorMessage { get; set; }
}


And he suggest to use this object as return type of each and every method of all the classes where we are expected to use try/catch in every method and also where ever these methods are called those are expected to check the response of previous method



Check sample below,



    public async Task<WrappedResult<List<AreaUserStagingDto>>> GetAreaUsersInStaging(UserDto currentUser)
{
WrappedResult<List<AreaUserStagingDto>> result = new WrappedResult<List<AreaUserStagingDto>>();
try
{
var query = null; //<= get output from database

result.Result = query;
}
catch (Exception ex)
{
_dbManager.HandleDbException(ex, ref result, currentUser);
}

return result;
}


I have following concerns with this way of exception handling.




  • Uselessly we will be using try/catch block through out the code base,
    whereas we should only handle exception in entry point or where we
    need to do something else on exception.

  • It will need extra lines of code for each method. Program need to check for
    errors every time even if there is no exception 99.9 percentage of time.


I think there could be more drawback of following this approach,



Does anyone see any benefit of this way of handling exception and returning WrappedResult.










share|improve this question





























    0















    Recently I have started building new application, this is a .Net Core based MVC and Webapi application.



    One of my senior came-up with Idea of WrappedResult as return type of each and every method. so that we need not to throw exception. Following is the structure of



    public class WrappedResult<T>
    {
    public T Result { get; set; }
    public bool IsSuccess
    {
    get
    {
    return !string.IsNullOrWhiteSpace(SuccessMessage);
    }
    }
    public string SuccessMessage { get; set; }
    public bool HasError
    {
    get
    {
    return Errors.Count > 0;
    }
    }

    public List<RectoError> Errors { get; set; } = new List<RectoError>();

    public void AddError(ErrorCode errorCode = ErrorCode.None, string errorMessage = null)
    {
    var rectoError = new RectoError()
    {
    ErrorCode = errorCode,
    ErrorMessage = errorMessage,
    };
    Errors.Add(rectoError);
    }

    public void AddError(RectoError rectoError)
    {
    Errors.Add(rectoError);
    }

    public Task<WrappedResult<T>> ToTask()
    {
    return Task.Factory.StartNew(() => {
    return this;
    });
    }
    }

    public class RectoError
    {
    public ErrorCode ErrorCode { get; set; }
    public Exception Exception { get; set; }
    public string ErrorMessage { get; set; }
    }


    And he suggest to use this object as return type of each and every method of all the classes where we are expected to use try/catch in every method and also where ever these methods are called those are expected to check the response of previous method



    Check sample below,



        public async Task<WrappedResult<List<AreaUserStagingDto>>> GetAreaUsersInStaging(UserDto currentUser)
    {
    WrappedResult<List<AreaUserStagingDto>> result = new WrappedResult<List<AreaUserStagingDto>>();
    try
    {
    var query = null; //<= get output from database

    result.Result = query;
    }
    catch (Exception ex)
    {
    _dbManager.HandleDbException(ex, ref result, currentUser);
    }

    return result;
    }


    I have following concerns with this way of exception handling.




    • Uselessly we will be using try/catch block through out the code base,
      whereas we should only handle exception in entry point or where we
      need to do something else on exception.

    • It will need extra lines of code for each method. Program need to check for
      errors every time even if there is no exception 99.9 percentage of time.


    I think there could be more drawback of following this approach,



    Does anyone see any benefit of this way of handling exception and returning WrappedResult.










    share|improve this question



























      0












      0








      0








      Recently I have started building new application, this is a .Net Core based MVC and Webapi application.



      One of my senior came-up with Idea of WrappedResult as return type of each and every method. so that we need not to throw exception. Following is the structure of



      public class WrappedResult<T>
      {
      public T Result { get; set; }
      public bool IsSuccess
      {
      get
      {
      return !string.IsNullOrWhiteSpace(SuccessMessage);
      }
      }
      public string SuccessMessage { get; set; }
      public bool HasError
      {
      get
      {
      return Errors.Count > 0;
      }
      }

      public List<RectoError> Errors { get; set; } = new List<RectoError>();

      public void AddError(ErrorCode errorCode = ErrorCode.None, string errorMessage = null)
      {
      var rectoError = new RectoError()
      {
      ErrorCode = errorCode,
      ErrorMessage = errorMessage,
      };
      Errors.Add(rectoError);
      }

      public void AddError(RectoError rectoError)
      {
      Errors.Add(rectoError);
      }

      public Task<WrappedResult<T>> ToTask()
      {
      return Task.Factory.StartNew(() => {
      return this;
      });
      }
      }

      public class RectoError
      {
      public ErrorCode ErrorCode { get; set; }
      public Exception Exception { get; set; }
      public string ErrorMessage { get; set; }
      }


      And he suggest to use this object as return type of each and every method of all the classes where we are expected to use try/catch in every method and also where ever these methods are called those are expected to check the response of previous method



      Check sample below,



          public async Task<WrappedResult<List<AreaUserStagingDto>>> GetAreaUsersInStaging(UserDto currentUser)
      {
      WrappedResult<List<AreaUserStagingDto>> result = new WrappedResult<List<AreaUserStagingDto>>();
      try
      {
      var query = null; //<= get output from database

      result.Result = query;
      }
      catch (Exception ex)
      {
      _dbManager.HandleDbException(ex, ref result, currentUser);
      }

      return result;
      }


      I have following concerns with this way of exception handling.




      • Uselessly we will be using try/catch block through out the code base,
        whereas we should only handle exception in entry point or where we
        need to do something else on exception.

      • It will need extra lines of code for each method. Program need to check for
        errors every time even if there is no exception 99.9 percentage of time.


      I think there could be more drawback of following this approach,



      Does anyone see any benefit of this way of handling exception and returning WrappedResult.










      share|improve this question
















      Recently I have started building new application, this is a .Net Core based MVC and Webapi application.



      One of my senior came-up with Idea of WrappedResult as return type of each and every method. so that we need not to throw exception. Following is the structure of



      public class WrappedResult<T>
      {
      public T Result { get; set; }
      public bool IsSuccess
      {
      get
      {
      return !string.IsNullOrWhiteSpace(SuccessMessage);
      }
      }
      public string SuccessMessage { get; set; }
      public bool HasError
      {
      get
      {
      return Errors.Count > 0;
      }
      }

      public List<RectoError> Errors { get; set; } = new List<RectoError>();

      public void AddError(ErrorCode errorCode = ErrorCode.None, string errorMessage = null)
      {
      var rectoError = new RectoError()
      {
      ErrorCode = errorCode,
      ErrorMessage = errorMessage,
      };
      Errors.Add(rectoError);
      }

      public void AddError(RectoError rectoError)
      {
      Errors.Add(rectoError);
      }

      public Task<WrappedResult<T>> ToTask()
      {
      return Task.Factory.StartNew(() => {
      return this;
      });
      }
      }

      public class RectoError
      {
      public ErrorCode ErrorCode { get; set; }
      public Exception Exception { get; set; }
      public string ErrorMessage { get; set; }
      }


      And he suggest to use this object as return type of each and every method of all the classes where we are expected to use try/catch in every method and also where ever these methods are called those are expected to check the response of previous method



      Check sample below,



          public async Task<WrappedResult<List<AreaUserStagingDto>>> GetAreaUsersInStaging(UserDto currentUser)
      {
      WrappedResult<List<AreaUserStagingDto>> result = new WrappedResult<List<AreaUserStagingDto>>();
      try
      {
      var query = null; //<= get output from database

      result.Result = query;
      }
      catch (Exception ex)
      {
      _dbManager.HandleDbException(ex, ref result, currentUser);
      }

      return result;
      }


      I have following concerns with this way of exception handling.




      • Uselessly we will be using try/catch block through out the code base,
        whereas we should only handle exception in entry point or where we
        need to do something else on exception.

      • It will need extra lines of code for each method. Program need to check for
        errors every time even if there is no exception 99.9 percentage of time.


      I think there could be more drawback of following this approach,



      Does anyone see any benefit of this way of handling exception and returning WrappedResult.







      exception-handling architecture asp.net-core-mvc






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 19:14







      Trikaldarshi

















      asked Dec 27 '18 at 9:07









      TrikaldarshiTrikaldarshi

      6,920145587




      6,920145587
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942431%2fexception-handling-in-everymethod-instead-throwing-and-wrappedresult-as-return-t%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53942431%2fexception-handling-in-everymethod-instead-throwing-and-wrappedresult-as-return-t%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas