End of file in Loop IF Error checking

Hello:
Reading the Clarion documentation I see the LOOP used to to iterate in a structure. Usually when I do that my code looksl ike this

SET(MasterFile)                !Point to first record
LOOP                           !Process all the records
  NEXT(MasterFile)             !read a record
  IF ERRORCODE() THEN BREAK.   !End of file check
  ProcMaster                   !call the procedure
END

But the documentation shows like this

SET(MasterFile)                   !Point to first record
LOOP                              !Process all the records
  NEXT(MasterFile)                !read a record
  IF ERRORCODE() = 33 THEN BREAK. !End of file check
  ProcMaster                      !call the procedure
END

What is the recommended way or the better way to do it and why?

Thanks
@ Urayoan

Hi,

I am using it like this:
SET(MasterFile) !Point to first record
LOOP WHILE ACCESS:MasterFile.NEXT() = LEVEL:BENIGN !Process all the records and automatically ends at end of file
ProcMaster !call the procedure
END

It has never failed me.

Regards
Johan de Klerk

2 Likes

Hi @Johan_de_Klerk

Does that work when a manual view is builded?

Thanks for the recommendation

I would go with the first IF ERRORCODE() THEN BREAK

When you hit the end of file you expect NEXT to throw error 33, that will be the norm. But NEXT can throw a few other errors (read the help) like 37 File Not Open which maybe some code closed it or it was never open. Driver Error 90 is likely corruption. If you don’t BREAK out of the LOOP on those other errors the loop will probably run forever.

If you wanted to be super careful you could check and report those other errors as shown below in the ELSE message. Instead of (or in addition to) the Message() add to a Problems Log file or OutputDebug.

NEXT (MasterFile)
CASE ERRORCODE()
OF NoError                      !0 No Error Occured
OF BadRecErr                    !33 Record Not Available
      BREAK
ELSE
   Message('Failed NEXT (MasterFile) ' & Err4Msg() )
   BREAK
END !CASE Error

Here’s a function to format an Error for a message I have in most of code:

  MAP
Err4Msg  PROCEDURE(Byte NoCRLF=0),STRING  !Fromat ErrorCode() & Error() & FileError... for Message() Stop() Halt() or Log file (NoCRLF)
  END
!-----------------------------------------
Err4Msg  PROCEDURE(Byte NoCRLF=0)!,STRING 
  !Example: IF ERRORCODE() THEN STOP('Failed ADD(xxx)' & Err4Msg()).
  !Note: Return starts '<13,10><13,10>Error Code:' so no need to put in the Message()
  CODE
  IF ~ERRORCODE() THEN RETURN ''.   
  IF ~NoCRLF THEN 
     RETURN '<13,10><13,10>Error Code: ' & ERRORCODE()&' '&ERROR() & |
             CHOOSE(~FILEERRORCODE(),'','<13,10>Driver Error: ' & FILEERRORCODE()&' '&FILEERROR() ) & | 
             CHOOSE(~ERRORFILE(),'','<13,10>File Name: ' & ERRORFILE() )
  END 
  !NoCRLF<>0 is 1 line format for use by logging
  RETURN ERRORCODE()&' '&ERROR() & |     ! {148}
         CHOOSE(~FILEERRORCODE(),'',' [Driver ' & FILEERRORCODE()&' '&FILEERROR() &']' ) & | 
         CHOOSE(~ERRORFILE(),'',' {{' & ERRORFILE() & '}' )

Adding & Err4Msg() with this function makes it so easy to include all the error details in message I use it very often. Call as Err4Msg(1) for One Line (no 13,10) of text like you would put in a log or OutputDebugString

6 Likes

Thank you very much @CarlBarnes.

The Help may not list all the errors that are possible. Windows can throw the RTL an error it does expect so does not map to a Clarion code in which case the ERRORCODE() will contain the windows error number. E.g. I have seen network connection errors.

1 Like