Forum Discussion

tristaanogre's avatar
tristaanogre
Esteemed Contributor
8 years ago

Re: How to do error checking on afFile or aqTextFile methods

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?

3 Replies

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

      Actually, many of those values you're looking for are available right from the Sys object.

       

      Perhaps you can skip, entirely, the process of reading the text file if the information you need can be obtained directly from ehre.