Scripting > mrScriptBasic overview > mrScriptBasic language reference > Statements > On Error
 
On Error
Enables error handling and specifies the location in the script to execute when an error occurs. On Error can also be used to disable error handling.
Syntax
On Error Goto location
On Error Resume Next
On Error Goto 0
Argument
location
The line label or section name to transfer execution to.
Remarks
If you do not use an On Error statement anywhere in your code, any run-time error that occurs causes script execution to be stopped and an error to be returned. The exact handling of the error is determined by the host running the script.
On Error Goto causes execution to continue at a different location in the script when an error occurs. The location specified will typically contain custom error handling for the script. The Err Object is used to determine the cause of the error. See IErrObject in the MDM Object Model Reference.
On Error Resume Next causes execution to continue with the statement immediately following the statement that caused the run-time error. This allows execution to continue despite a run-time error. You can then build the error-handling routine inline within the script.
You can use On Error Goto 0 to disable error handling if you have previously enabled it using On Error Goto, or On Error Resume Next. To prevent the possibility of infinite recursion, On Error Goto 0 is implicit when entering the error handler.
Examples
Using On Error Goto
This example includes some script that will give a “Division by zero” error. The example uses On Error Goto to pass control to a section of the script that handles errors and which, in this example, writes the error number and description to a text file.
Dim fso, txtfile, ErrorText, x, y, z

On Error Goto ErrorHandler

' Prepare the output file
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("errors.txt", True)

x = 30
y = 0
z = x / y

Exit ' Success

ErrorHandler:
ErrorText = CText(Err.Number) + ": " + Err.Description
txtfile.WriteLine (ErrorText)
ErrorText = ""
The CreateObject function creates the FileSystemObject, which is a standard Microsoft object for working with folders and files.
The CText function converts the Err.Number property to a string for including in the text file. See also IErrObject_Number in the MDM Object Model Reference.
Using On Error Resume Next
This example is similar to the previous one, except that this time it uses On Error Resume Next and tests for errors within the main part of the script. It tests for errors by testing whether the property is unequal to zero.
Dim fso, txtfile, ErrorText, x, y, z

On Error Resume Next

' Prepare the output file
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("errors.txt", True)

x = 30
y = 0
z = x / y

If Err.Number <> 0 Then
ErrorText = CText(Err.Number) + ": " + Err.Description
txtfile.WriteLine (ErrorText)
ErrorText = ""
End If
Using On Error Goto 0
This example is the same as the previous one, except it includes the line On Error Goto 0 to switch off error handling.
Dim fso, txtfile, ErrorText, x, y, z

On Error Resume Next

' Prepare the output file
Set fso = CreateObject("Scripting.FileSystemObject")
Set txtfile = fso.CreateTextFile("errors.txt", True)

x = 30
y = 0
z = x / y

If Err.Number <> 0 Then
ErrorText = CText(Err.Number) + ": " + Err.Description
txtfile.WriteLine (ErrorText)
ErrorText = ""
End If

On Error Goto 0
.
.
.
Using On Error in an interview script
On Error Resume Next
Rating[..].Ask()

' Handle any errors
If Err.Number <> 0 Then
' Count the number of errors
ErrorCount = ErrorCount + 1
End If

On Error Goto ErrHandler

Drinks.Ask()

SummaryQuestions:

On Error Goto 0

' Ask the standard demographic questions

Demo.Ask()
Exit

ErrHandler:
' Log the error and run the summary questions
Err.Log(mrStatusLog)
Goto SummaryQuestions
See also
Statements