try-catch blocks with the return type

后端 未结 11 1933
半阙折子戏
半阙折子戏 2021-02-07 14:02

If I have a method that returns something, like

public DataTable ReturnSomething()
{
   try
   {  
      //logic here
     return ds.Tables[0];
   }
   catch (Ex         


        
11条回答
  •  清歌不尽
    2021-02-07 14:51

    If you are going to head the "don't throw an exception route" (which I am not necessarily reccomending), you could follow the TryParse approach MS uses.

    Something like:

    private string FillDataTable(out DataTable results)
    {
    
      try
    {
      results = new DataTable(); //something like this;
      return String.Empty;
    }
    catch (Exception ex)
    {
      results = null;
     return ex.Message;
    
    }
    

    }

提交回复
热议问题