Recusive calls from a Catch block
===


I'm coding a method where an attempt to upload a document is wrapped within a Try/Catch block.

If the attempt fails, I'm incrementing the Retry counter and recursively calling the same method.

I'm not clear on the execution path in those cases where the 'Catch' block is hit. Initial tests show that the 'return null' statement is executed after that first recursive call executes. Perhaps the 'return null' statement needs to be within the Catch block but outside the retries<3 loop?




    public RssUploadDocOutput UploadInvoice(string filename)
            {
                var returnsOutput = new RssUploadDocOutput();
    
                GoogleSheetsCommand sscmd = new GoogleSheetsCommand("UploadDocument", ConnSheets);
                sscmd.CommandType = System.Data.CommandType.StoredProcedure;
                sscmd.Parameters.Add(new GoogleSheetsParameter("LocalFile", filename));
    
                int retries = 0;
    
                try
                {
                 GoogleSheetsDataReader rdr = sscmd.ExecuteReader();
                   rdr.Read();
    
                    returnsOutput.ID = rdr[0].ToString();
                    [...]
                    returnsOutput.Weblink = rdr[6].ToString();
    
                    return returnsOutput;
                }
                catch (Exception ex)
                {
                    retries++;
                    Logger.Instance.LogException(ex);
                    if (retries < 3)
                    {
                        Thread.Sleep(1000 * retries);
                        UploadInvoice(filename);
                    }
                    else
                    {
                        return null;
                    }
                }
                return null;
            }