WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Error Codes

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

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 :WBERRORHANDLER
This 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
wberrorhandlerfile        string   Name of the currently-executing script
wberrortextstring         string   Description of the WIL error
wberroradditionalinfo     string   Additional error information, if any
wberrorinsegment          string   name of the currently-executing UDF, or a blank string ("") if not in a UDF.
wberrorarray              array    WIL array with the following elements:
                                   wberrorarray[0] = LastError()
                                   wberrorarray[1] = wberrorhandlerline
                                   wberrorarray[2] = wberrorhandleroffset
                                   wberrorarray[3] = wberrorhandlerassignment
                                   wberrorarray[4] = wberrorhandlerfile
                                   wberrorarray[5] = wberrortextstring
                                   wberrorarray[6] = wberroradditionalinfo
                                   wberrorarray[7] = wberrorinsegment

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 :

http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/DOS+Dos~Errorlevel~Return~Code.txt


Article ID:   W12975
Filename:   How do I trap errors.txt
File Created: 2014:07:18:09:51:38
Last Updated: 2014:07:18:09:51:38