try-catch blocks with the return type

后端 未结 11 1926
半阙折子戏
半阙折子戏 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

    How about this :

    public DataTable ReturnSomething(out string errorString)
    {
       errorString = string.Empty;
       DataTable dt = new DataTable();
       try
       {  
          //logic here
         dt = ds.Tables[0];
       }
       catch (Exception e)
       {
          errorString = e.Message;
       }
       return dt;
    }
    

提交回复
热议问题