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 wish this were the case, that's not the real world:  files that should exist don't, access privileges to write to a file are sometimes, wrong, etc.

  • 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; }
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    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?

    • Dan_Gawarecki's avatar
      Dan_Gawarecki
      Occasional Contributor

      Hi Robert:

      Thanks for the quick response!

       

      "Try/Catch/Finally" login would be just fine - I just wasn't sure what was available (I did do a search on the forum and came up empty).

       

      I'm doing

      var propertyFile = aqFile.OpenTextFile(sPropFilePath, aqFile.faRead, aqFile.ctUTF8)

      so I can read a few bits of information local to the workstation (e.g,. TechName, Machine Name, etc) so the tech don't have to keep typing it in for each test (they're coded to be very independent).

       

      There's probably a better way to do this, but that's my ignorant approach at the moment.

       

      • shankar_r's avatar
        shankar_r
        Community Hero

        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; }