Is the [error level / exit code] optional for the EXIT command?
Yes and No!
When KiX encounters the EXIT command, it processes what follows it as though it were an optional expression but if the optional expression is not provided, it processes whatever inline code it thinks to be the optional expression.
Take the following example:
The first test called in the GOSUB spawns a MesageBox, then the second since it is before the exit, but what some might not expect is that the first test is spawned yet again.
Even if we handle the return codes from the MessageBox() function, it still does the same. KiX does not know where to stop and goes on to enumerate expressions that follow the EXIT.
The solution is to ALWAYS include the not-so-optional value or expression.
When providing the parm, one should try to remain true to the meaning of the EXIT code. Also, remember that EXIT is used in different places with different results. It does NOT always EXIT KiX. If you truly wish to EXIT KiX, use the QUIT command but don't forget to pass a parm to is as well!
The value passed to EXIT and/or QUIT will set the environment %ERRORLEVEL% to that value when it EXITs KiX. Knowing that, if KiX is called via a BATch file, there can be some error logging and/or recovery. See the FAQ Catch that error message that flashes for a second
BTW, did you know that MessageBox() will set @Error? It can really throw your code for a loop if you LOOP on @Error=0! That is a topic for another FAQ, but while I am here I may as well share the following example of using EXIT to zero out @Error.
'Dang! look what MessageBox() does to @@Error... ' + @error ?
'See what ZeroError does... ' +@error ?
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.