Error Trapping
Keywords: error trapping ErrorMode Lasterror
Question:
How do I trap errors?Answer:
Note: Starting in WinBatch version 2000B, see the function IntControl(73, p1, 0, 0, 0). IntControl 73 sets Error handler.
This IntControl lets you specify what should happen when the next error occurs in the script.
P1 Meaning -- ------- -1 Don't change (just return current setting) 0 Normal error processing (default) 1 Goto the label :WBERRORHANDLER 2 Gosub the label :WBERRORHANDLERThis is a one-time flag, which gets reset to 0 after the next error occurs.When processing goes to :WBERRORHANDLER, the following WIL variables are automatically set:
Variable Type Meaning -------- ---- ------- wberrorhandlerline string error line (ie, line in script that caused error) wberrorhandleroffset integer offset into script of error line, in bytes wberrorhandlerassignment string variable being assigned on error line, or "" if none Note: The :WBERRORHANDLER label must be in the same script where the error occurred. If an error occurs in a called script, it will go to the label in the called script, not the calling script.Returns previous setting.*OR*
There are a couple of ways. With the IntControl(1000, p1, 0, 0, 0) command you can set an exit code returned by Winbatch. However, this return code can ONLY be read by DOS batch files, not Winbatch itself or other Windows application.For logging Windows errors, I suggest setting a value in an ini file. The subject program would occasionally poll the ini file and when it sees the value, does what you want.
or......
In front of the Run Command, do a call to the last error function.
Example:
LastError() ;this sets the error memory to zero ErrorMode(@OFF) ;this tells the WB we are handling the errors ;ourselves and not to bother the script. Run(statement) ;just like it originally was) ErrorMode(@CANCEL) ;this hands control back to WB which will look for ;error messages. If LastError !=1932 then Message("Script Died","Error not 1932" ) then Exit ;this stomps on other errors but lets 1932 go through ;so it is never seen. ; Note: != means Not Equal.or.....If you're trying to get returned error levels from a DOS program, see the sample in the tech support area under WinBatch/DOS:
Article ID: W12975Filename: How do I trap errors.txt