- The operator forgot to fill in a Setpoint and then hit a button;
- The script required the value of an IOTag, however the communication with the PLC was interrupted at that moment;
- The script attempted to write a record in a table, but the connection to the Database was interrupted at that moment.
Most of the times, it becomes an inconvenience. The operator may not know what happened, whether it was an irrelevant failure or a severe error.
However, vbScript features a few resources that enable the developer to create their own logic for handling errors. Some of them are discussed below.
On Error Resume Next
On Error instruction is used to indicate which action must be taken by VB. If you use “On Error Resume Next”, the action taken will simply be to continue on the next line, which means the script error will be ignored.
This is valid for all lines below the instruction.
On Error Goto 0
This instruction nullifies “On Error Resume Next”. This means that the lines below “On Error Goto 0” errors will be handled in the traditional way: an “error message” will be displayed, and the script will be aborted.
“Err” Object
This object represents the script error. You can use its properties to identify what went wrong during execution.
To understand this better, please refer to the next script handling example:
'From this line on, errors will be ignored
On Error Resume Next
Application.GetObject("Data.NonexistentTag1").Value = 1
Application.GetObject("Data.NonexistentTag 2").Value = 1
Application.GetObject("Data.NonexistentTag 3").Value = 1
'…
'…
If Err.Number <> 0 then
MsgBox "Attention: there has been an error while executing this script: " & vbNewLine &_
Err.Number & "-" & Err.Description & vbNewLine & _
"Please contact the developer responsible."
End If
Exit Sub
“Exit Sub” instruction is used to abort the script.
In the script example below, you can see that it is possible to predict a script error without resorting to the instructions described previously.
If Screen.Item("Setpoint1").Value = "" then
MsgBox "Fill the Setpoint before hitting the button!"
Exit Sub
End If
'...
'more script
'...
The example above would solve situation “1.”, described at the beginning of this article.
For further information on vbScript, please refer to vbScript’s Reference Guide.
The following instruction does not work: On Erro GoTo 0
Maybe this link explains the reason:
https://docs.microsoft.com/en-us/sql/ado/guide/data/handling-errors-in-vbscript?view=sql-server-ver15
Marco,
You cannot use the ‘On Error GoTo line‘ statement in VBScript. The ‘On Error GoTo 0’ statement works properly to cancel the effect of a previous ‘On Error Resume Next’ statement.