try-catch blocks with the return type

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

    Store your return value in a temporary variable like this:

    public DataTable ReturnSomething()
    {
        DataTable returnValue = null;
    
        try
        {
            //logic here
            returnValue = ds.Tables[0]; 
        }
        catch (Exception e)
        {
            ErrorString=e.Message;
        }
    
        return returnValue;
    }
    

提交回复
热议问题