Handling script errors.

A script error can happen for several different reasons. Some examples are:
  1. The operator forgot to fill in a Setpoint and then hit a button;
  2. The script required the value of an IOTag, however the communication with the PLC was interrupted at that moment;
  3. The script attempted to write a record in a table, but the connection to the Database was interrupted at that moment.
For either situation, vbScript’s standard procedure is: display an error message, and abort script.

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.

Print Friendly, PDF & Email

Este artigo foi útil? Was this helpful?

Classificação média - Average rating 0 / 5. Count: 0

Thoughts on “Handling script errors.

Leave a Reply

Your email address will not be published.Required fields are marked *