Home > Language Reference > Program Flow Functions > OnAction Error

OnAction Error

The OnAction Error...EndAction function handles error events. It is a specific OnAction for managing execution errors.

Usage

Used for calling an error procedure whatever unexpected error occurs during script execution. You can for example take a screenshot of the desktop when an execution error occurs

Syntax

OnAction Error
    DoSub <sub_name>
    EndAction
 

Parameters

<sub_name>, constant, subroutine to be executed as soon as an execution error occurs. The Sub cannot have parameters.

Remarks

If an error occurs during execution of the script, the procedure <sub_name> is immediately executed, then the script continues. This <sub_name> is a subroutine without any parameters.

Only one error handler is active at a time: so if a new one is defined in the script, it's the latest one which is used when an error occurs after the definition of this new OnAction Error.

The error handler (and the execution of the associated subroutine) is activated as soon as the statement OnAction Error has been interpreted in the script. It stays active until the script ends OR until a new statement OnAction Error is encountered in the script.

If a script launches another script (using the Run function), the error handler in the parent script is not active anymore during execution of the child scripts.
 
It is possible to deactivate the error handler by inserting a Disable(Error) function, but this function must be inserted in the script in which the error handler was defined (not in a parent/child script). It can be activated again by inserting a Enable(Error) function.

See also

#ErrorCode
#ErrorFunction$
#ErrorMsg$
#ErrorScript$
#LastErrorLine
#LastErrorLine$

Examples

'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:\wttest\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")
 

'A full script which in case of any execution error captures the desktop, so if the script runs unattended, you will know how was the entire desktop when the error occurred.

'Definition of the proc to call if an error is detected.
Sub Process_Error()
local buffer$
 
'If within this proc, an error occurs too, it generates an infinite loop as
'the call to the error proc causes an error. To avoid that, we disable OnAction Error within this proc.
disable(Error)
'Take a screenshot of the desktop
ret=Hardcopy("C:\program files (x86)\ wintask\scripts\error.jpg",1,1)
 
'Here a part for writing in a txt file where we were in the script, the error code, the error message when the error occurred.
buffer$="Error in : "+#ErrorScript$
write("C:\program files (x86)\ wintask\ scripts\error.txt",buffer$,crlf)
buffer$="Error line : "+str$(#LastErrorLine)
write("C:\program files (x86)\ wintask\ scripts\error.txt",buffer$,crlf)
buffer$="Error Code : "+str$(#Errorcode)
write("C:\program files (x86)\ wintask\ scripts\error.txt",buffer$,crlf)
buffer$="Error Function : "+#ErrorFunction$
write("C:\program files (x86)\ wintask\ scripts\error.txt",buffer$,crlf)
buffer$="Error message : "+#ErrorMsg$
write("C:\program files (x86)\ wintask\ scripts\error.txt",buffer$,crlf)
 
 
'Add here other lines for what to do in case of an error.
'......
 
Endsub
 
 
OnAction error
dosub Process_Error
endaction
 
#Actiontimeout=15
'The window is not found, so OnAction error triggers.
UseWindow("toto")