Error Handling

When a script is running and an error occurs, the default WinTask behavior is to display an error message and to stop all the currently running scripts.

It is possible to change this behavior by setting the system variable #IgnoreErrors to 1 (its default value is 0).

If #IgnoreErrors=1, and an error occurs the script continues. That can be dangerous and so we recommend that you test the return code of each function to know if the function has been correctly executed (for example, ret=UseWindow(<window_name>)). If this system variable is set to 1, the programmer must manage the error situation. To set up #IgnoreErrors can be done anywhere in the script, so you can go back and forth from 0 value to 1 value. See a code example below.

If a function does not have a return code, such as those returning a numeric or a string, with #IgnoreErrors=1, the script continues. Look at each individual function to see the value returned when an error occurs.

The system variable #ActionTimeout specifies the number of seconds which WinTask should wait before reporting a runtime error or raising a non-zero return code when it tries to select a menu option, a window or a control.

 

Instead of testing each individual return code for each function when you use #IgnoreErrors=1, you can implement a general Error Handling procedure (not available in WinTask Lite) through OnAction Error statement. See the second code example below.

See also

Script detecting an error window displayed randomly

Debug mode

OnAction Error

#ScriptAfterTimeout$

 

The example below shows error handling by testing the return code of a WinTask function, the next one shows how to use an error subroutine when the return code of a WinTask function is not zero, the third one shows a call to an error subroutine when a pause fails (and it returns an exit code), the fourth one shows the use of OnAction Error, which launched an error subroutine as soon as an error occurs.

 

Error handling testing the return code of a WinTask function:

 

#ActionTimeout=10

' If the target of an action is not found after 10 seconds, an error will occur

 

UseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)

' If the UseWindow does not succeed after 10 seconds, an error message is displayed and all scripts are stopped, because the default value of #IgnoreErrors is 0.

 

#IgnoreErrors=1

' Now, the programmer will handle the errors and the script will continue when an error occurs.

 

ret=ChooseMenu(Normal,"&File|&Open...")

' We select a menu option

 

' Return code is tested

if ret=0 then

msgbox("Menu option found")

UseWindow("NOTEPAD.EXE|#32770|Open",1)

Click(Button,"Cancel")

else

msgbox("Menu option not found")

endif

 

#IgnoreErrors=0

' Now default error handling is activated.

 

Error handling testing the return code of a WinTask function, and when an error is detected, call an error subroutine (not available in WinTask lite) :

 

 

Sub OnError()

msgbox("error found")

EndSub

 

#ActionTimeout=10

' If the target of an action is not found after 10 seconds, an error will occur

 

#IgnoreErrors=1

' Now, the programmer manages the errors and the script will go on even if an error occurs.

 

ret=shell("notepad.exe")

 

' Return code is tested

if ret=0 then

msgbox("notepad has been launched successfully")

else
'Call the error subroutine

OnError()

endif

 

 

Error handling calling an error subroutine if an error occurs in a Pause block (not available in WinTask lite):

 

Sub process_error(event$,exit_code)
Comment("Error at line "+#ErrorLine$+": "+event$)
MsgFrameTitle(event$,event$,1,,,14)
Pause 1 Secs
RemoveFrame(1)
'You can call a new script which name is in cleanupScript$: when an error occurs, you clean the desktop using for example KillApp
Run(cleanupScript$)
'The exit_code can be used in another program.
End(exit_code)
EndSub

Shell("notepad")

UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad")
SendKeys("Hello")

CloseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)

Pause 5 until
WinStatus(Active)
InWindow("NOTEPAD.EX|#32770|Notepad",1)
PauseFalse
' the synchro failed, set up the exit code, and call the error proc.

exit_code=1
process_error("NOTEPAD.EX|#32770|Notepad window not found",1)
EndPause

 

Error handling calling an error subroutine as soon as an error occurs (not available in WinTask lite) :

 

'General error procedure which is called when an error occurs.

'In this proc, we just log the error and execution goes on.

Sub error_proc()

Comment(#ErrorFunction$+" at line "+#LastErrorLine$+" : "+#ErrorMsg$)

EndSub

 

'As soon as an error occurs, the procedure defined there is called.

OnAction Error

DoSub error_proc

EndAction

 

#ActionTimeout=10

LogFile("c:\my_log.txt",0,1)

 

Shell("wordpad",1)

UseWindow("WORDPA.EXE|RichEdit20W|Document - WordPad|1",1)

SendKeys("aaaaaa")

 

UseWindow("WORDPAD.EXE|WordPadClass|Document - WordPad",1)

ChooseMenu(Normal,"&File|E&xit")

 

UseWindow("WORDPAD.EXE|#32770|WordPad",1)

Click(Button,"&No")