try-catch blocks with the return type

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

    You can do it like the sample code below.

    public DataTable ReturnSomething(out string OutputDesc)
    {
       try
          {
             //logic here
             OutputDesc = string.Format("Your Successful Message Here...");
             return ds.Tables[0];
          }
          catch (Exception e)
          {
             OutputDesc =e.Message;
             return null;
          }
    
    }
    

提交回复
热议问题