Forum Discussion

Dan_Gawarecki's avatar
Dan_Gawarecki
Occasional Contributor
8 years ago

How to do error checking on afFile or aqTextFile methods

In using afFile or aqTextFile methods, what error checking can I do to ensure the method succeeded?  The code provided in TC assumes a "happy path"; that is, that no errors will ever happen.  While I...
  • tristaanogre's avatar
    8 years ago

    It depends a lot upon the methods you are using.  The first thing I'd start with is to wrap them with "try/catch/finally" logic to trap anything unexpected.  You are correct, the documentation shows "happy path" but all that is intended to show is "usage", not good coding practices. 

     

    Do you have a specific code example of what you want to mitigate?

  • shankar_r's avatar
    shankar_r
    8 years ago

    Hi,

     

    I had kind of same scenario, I use below code to do that.

     

    function test()
    {
          CheckFile("C:\\Test\\Test.txt");
    } 
    function CheckFile(pathofthefile)
    {
          if(aqFile.Exists(pathofthefile)) //Or if(aqFileSystem.Exists(pathofthefile))
          {
                Log.Checkpoint("File exists in the path " + pathofthefile);
                
                var txtFile = aqFile.OpenTextFile(pathofthefile,aqFile.faRead, aqFile.ctUnicode);
                
                try
                {
                      if(txtFile.LinesCount > 0)
                      {
                            Log.Checkpoint("Text file contains " + txtFile.LinesCount + " lines");    
                            return true; 
                      } 
                      else
                      {
                            Log.Warning("Text file is empty");
                      } 
                }
                catch(e)
                {
                      Log.Error("Unexpected error : "  + e.stack);
                } 
                    
          } 
          else
          {
                Log.Error(pathofthefile + " file not exists")
          } 
    return false; }