Professional > Interview scripting > Writing interview scripts > Dealing with errors > Handling script errors > Script to demonstrate script errors
 
Script to demonstrate script errors
This topic is designed for people who are new to scriptwriting. It contains a simple script that you can run to generate script errors and then change to see the difference between using and not using an error handler. The script generates a type mismatch and an out-of-range subscript, and contains an error handler that displays the error message. If you're using UNICOM Intelligence Professional to write and test scripts, the error message appears in the Output pane.
To see how this code works, copy it into a new script file and then comment out different sections as suggested in the comments in the script. The metadata section is:
Q1 "This is Q1" long [1 .. 9];
Q2 "This is Q2" long [1 .. 9];
The routing section is:
' Remove the comment from the next line to use the error handler
' On Error Goto ErrorHandler

Q1.Ask()

' Define some variables for use in the script
Dim txtvar[3] ' Generates txtvar[0] to txtvar[2]
Dim numvar
numvar = 0

' Type mismatch error if you use the next line instead of one below it
' txtvar[numvar] = "Some text" + numvar
txtvar[numvar] = "Some text" + ctext(numvar)

For numvar = 0 to ubound(txtvar)-1
' Subscript out of range with this logic because there is no txtvar[3]
' Remove the comment from the next three lines to see the error
'If numvar = 1 Then
' numvar = 3
'End If
Debug.Log("txtvar contains " + txtvar[numvar])
Next

Q2.Ask()
Exit

ErrorHandler:
Debug.Log("ERROR HANDLER: The error is " + err.Description)
' Remove the comment from the next statement to continue the interview
' after the error
' Resume Next
Here are some suggestions on how you can use this script to test various error scenarios.
If you run the script exactly as it is, you will see Q1. Answer the question and click Next. The script will stop with the message “Subscript out of range accessing the array variable 'txtvar'”. If you are using UNICOM Intelligence Professional you'll see that this message appears when the Debug statement in the For...Next loop is executed.
Now change the commenting in the section that will produce a type mismatch. When you run the script you'll see Q1 and then the interview will end with the message “Type mismatch error, converting 'Some text' from text to Double”.
Now remove the comment marker from the On Error statement. When you run the script this time, errors will be directed to the error handler so you'll see Q1 and then the interview ends with a message that says “ERROR HANDLER: The error is Type mismatch error, converting 'Some text' from text to Double”. If you were to change the error message to say “An internal error means that the interview cannot continue. Thank you for your help.”, you have an error handler that you can use in your scripts.
All the interviews so far have ended when the error occurred, so you've never reached Q2. If you want interviews to continue after an error, you need to instruct the interviewing program to do this. To see this for yourself, remove the comment marker from the Resume Next statement. This tells the interviewing program to resume the interview with the statement after the one that caused the error. When you run the script now, you'll see Q1, the message box with the error message, and then Q2.
See also
Handling script errors