Handling Errors

 

Next Topic

The WIL Tutorial

§         Topic by Topic

§         The Tutorial Course

§         The Complete WIL Tutorial

 

 

Getting started

Using WIL

Reference

Notes

 

Luckily, there is a WIL system function to help us here: ErrorMode. The ErrorMode function lets you decide what will happen if an error occurs during WIL processing. Here's the syntax:

 

ErrorMode(mode)

Specifies how to handle errors.

Parameters:

(i) mode = @CANCEL, @NOTIFY, or @OFF.

Returns:

(i) previous error setting.

 

Use this command to control the effects of runtime errors. The default is @CANCEL, meaning the execution of the WIL program will be canceled for any error.

@CANCEL: All runtime errors will cause execution to be canceled. The user will be notified which error occurred.

@NOTIFY: All runtime errors will be reported to the user, and they can choose to continue if it isn't fatal.

@OFF: Minor runtime errors will be suppressed. Moderate and fatal errors will be reported to the user. User has the option of continuing if the error is not fatal.

 

As you can see, the default mode is @CANCEL, and it's a good idea to leave it like this. However, it is quite reasonable to change the mode for sections of your WIL program where you anticipate errors occurring. This is just what we've done in our modified WIL program:

 

If FileExist("wil.bak") == @TRUE
 ErrorMode(@OFF)
 FileDelete("wil.bk5")
 FileRename("wil.bk4", "wil.bk5)
 FileRename("wil.bk3", "wil.bk4)
 FileRename("wil.bk2", "wil.bk3)
 FileRename("wil.bk1", "wil.bk2)
 FileRename("wil.bak", "wil.bk1)
 ErrorMode(@CANCEL)

Endif

Run("winword.exe", "wil.doc")
Exit

 

Notice how we've used ErrorMode(@OFF) to prevent errors in the If statement section from aborting the WIL program, and then used ErrorMode(@CANCEL) at the end of the that section to change back to the default error mode. This is a good practice to follow.

Note: Pay close attention when suppressing errors with the ErrorMode function. When an error occurs, the processing of the ENTIRE line is canceled. Setting the ErrorMode( ) to @OFF or @NOTIFY allows execution to resume at the next line. Various parts of the original line may have not been executed.

 

e.g.

ErrorMode(@OFF)
; The FileCopy will cause a file not found error,
; canceling the execution of the whole line.
; The variable A is set to @FALSE by default
 A = FileCopy( "xxxxxxxxx", "*.*", @FALSE)
;
;
; Now there is a NOT symbol in front of the FileCopy.
; Nonetheless, if an error occurs A is still set to @FALSE
; not @TRUE as might be assumed. When an error is suppressed
; with ErrorMode the line is canceled, and any assignment is
; simply set to the default @FALSE value.
;
 A = !FileCopy("yyyyyyyyy", "*.*", @FALSE)

 

For this reason, ErrorMode( ) must be used with a great deal of care. The function for which the errors are being suppressed should be isolated from other functions and operators as much as possible.

e.g.

; INCORRECT USAGE of ErrorMode( )
; In this instance, when the copy has an error, the entire if
; statement is canceled.
; Execution begins (erroneously) at the next line, and states
; that the copy succeeded. Next a fatal error occurs as the
; "else" is found, since it does not have a matching if

ErrorMode(@OFF)
if FileCopy(file1,file2,@FALSE) == @TRUE 
 Message("Info", "Copy worked")
else
 Message("Error", "Copy failed")
endif

; CORRECT USAGE
; In this case, the FileCopy is isolated from other statements
; and flow control logic. When the statement fails, execution
; can safely begin at the next line. The variable "a" will
; contain the default value of zero that a failed assignment
; returns.
; Results are not confused by the presence of other operators.
;
ErrorMode(@OFF)
a = FileCopy(file1,file2,@FALSE)
ErrorMode(@CANCEL)
if a == @TRUE
 Message("Info", "Copy worked")
else
 Message("Error", "Copy failed")
endif