Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Processing multiple files even after validation of 1 file errors out

Posted on by

Hi experts,

My code processes all xls files in a specific folder & while processing per file  if there is any validation failure it has to stop processing that file , however continue to process remaining files.

With below psuedo code of my original code , you would notice that objective 1 >Which is to stop processing the file as soon as there as an error is successfully met, however unable to meet objective 2 >which is that on an event file 1 errors out , continue to process file2 . I tried using checkfail but i m sure i did not use correctly because i was using checkfailed in place of throw error  in catch block .

Could you please suggest a code/psuedo code fix to achieve objective# 2

Class myClass
{
public run()
{
    System.IO.DirectoryInfo     directory;
    System.IO.FileInfo[]        files;
    System.IO.FileInfo          file;
    InteropPermission           permission;
    userid                      userid;
    MyTable                     myTable;
    counter                     filesCount;
    FilePath                    processedFilePath,processedFileCompletePath;
    Filename                    filename;
    Filename                    tmpFilePath = System.IO.Path::GetTempPath();
    Filename                    tmpFileNameShort;
    Filename                    tmpFileExt;
    counter                     loop;
    str                         fileNameTemp,fileNameString;

    #FILE

    try
    {
        userid = curuserid();

        if (userid != 'John Doe')
        {
            throw error   ('You are not logged in as John Doe');

        }

        select   myTable;

        directory   = new System.IO.DirectoryInfo(@myCustomTable.myPath);
        processedFilePath = @myCustomTable.myPath2;

        if ( directory == null || processedFilePath == '')
        {
            if (directory == null)
            {
               throw error  ('myPath is null');
            }

            else
            {
                throw error  ('myPath2 is undefined');
            }
        }



        files       = directory.GetFiles('*.xls*'); 
        filesCount  = files.get_Length();

        if (filesCount == 0)
             throw error('No file found' ,add the file & restart the process');

        for (loop = 0; loop < filesCount; loop  )
        {
            file = files.GetValue(loop);
            fileNameTemp = file.get_FullName();

            filename = fileNameTemp;

            if (strLen(fileNameTemp) > 0)
            {
                [tmpFilePath, tmpFileNameShort, tmpFileExt] = fileNameSplit(fileNameTemp);
                fileNameString= tmpFileNameShort   tmpFileExt;

                info(strFmt('Import filename=%1', fileNameTemp));

                if(tmpFilePath && tmpFileNameShort && tmpFileExt == #xls)
                {

                    this.methodA();
                }

            }


    }// for ends

} // Try ends

    catch (Exception::CLRError)
    {

        throw error ('CLR ERROR Details '  CLRInterop::getLastException().ToString());
    }

    catch
    {

        throw error(NON CLR ERROR);
    }
} // run ends


    public methodA()
    {
        try
        {
           //Do somthing
            this.methodB()
        }
        catch
        {
             throw error ("Method A failed");
        }
            
    }// method A ends


    public methodB(v1,v2,v3)
    {
     try
     {
        Do something
          if (v1 > 10)
          {
            throw error ("V1 geater than v1);
          }
          else if (v2 =='')
          {
            throw error ("V2 is blank");
          }
          else
          {
          //do something
          }
        }
        catch
        {
         throw error ("Method b failed");
        }
    
    } // method B ends
}

Thanks

Mav

  • Verified answer
    Sergei Minozhenko Profile Picture
    Sergei Minozhenko 23,089 on at
    RE: Processing multiple files even after validation of 1 file errors out

    Hi Mav,

    At the moment you have one "global" try-catch which catches exception and exit method. If you want to keep processing even one file fails, you need to add one more try-catch inside the loop, but don't throw errors in the catch part for the new try-catch block.

    try
        {
            userid = curuserid();
    
            if (userid != 'John Doe')
            {
                throw error   ('You are not logged in as John Doe');
    
            }
    
            select   myTable;
    
            directory   = new System.IO.DirectoryInfo(@myCustomTable.myPath);
            processedFilePath = @myCustomTable.myPath2;
    
            if ( directory == null || processedFilePath == '')
            {
                if (directory == null)
                {
                   throw error  ('myPath is null');
                }
    
                else
                {
                    throw error  ('myPath2 is undefined');
                }
            }
    
    
    
            files       = directory.GetFiles('*.xls*'); 
            filesCount  = files.get_Length();
    
            if (filesCount == 0)
                 throw error('No file found' ,add the file & restart the process');
    
            for (loop = 0; loop < filesCount; loop  )
            {
                try //Additional try-catch
                {
                    file = files.GetValue(loop);
                    fileNameTemp = file.get_FullName();
        
                    filename = fileNameTemp;
        
                    if (strLen(fileNameTemp) > 0)
                    {
                        [tmpFilePath, tmpFileNameShort, tmpFileExt] = fileNameSplit(fileNameTemp);
                        fileNameString= tmpFileNameShort   tmpFileExt;
        
                        info(strFmt('Import filename=%1', fileNameTemp));
        
                        if(tmpFilePath && tmpFileNameShort && tmpFileExt == #xls)
                        {
        
                            this.methodA();
                        }
        
                    }
                }
                catch
                {
                    //Catch errors for each file, but do not throw errors 
                }
    
    
        }// for ends
    
    } // Try ends
    
        catch (Exception::CLRError)
        {
    
            throw error ('CLR ERROR Details '  CLRInterop::getLastException().ToString());
        }
    
        catch
        {
    
            throw error(NON CLR ERROR);
        }
    } // run ends

  • Gunjan Bhattachayya Profile Picture
    Gunjan Bhattachayya 35,421 on at
    RE: Processing multiple files even after validation of 1 file errors out

    Hi Mav,

    To process all the files, you will need to call the statement "continue" in the catch statement. But, since you have the statement "throw error", the execution stops right there. Can you store the errorLog and handle the errors at the end?

    Please check this post for an example of using the continue statement.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans