Handling Errors

Handling compile-time and runtime errors in the MSScript Demo program is really a two-step process. I’ve already pointed out the first step, which is to use an On Error statement with the AddCode, Eval, ExecuteStatement, and Run methods. This prevents a runtime error from stopping your application. The other step is to use the Error event to trap runtime and compile-time errors in your script program. Figure 13.10 shows a typical compile-time error that is trapped and reported to the user.

Figure 13.10: Trapping a compile-time error

The Error event is triggered when an error occurs in your script, either at compile-time or at runtime. The complete information about the error is contained in the Error object. As you can see in Listing 13.5, in this event, I choose to display the line that contains the error (ScriptControl1.Error.Text) and the description of the error (ScriptControl1.Error.Description). After I display the error message, I clear the error using the Clear method.

Listing 13.5: ScriptControl1_Error Event in MSScript Demo

Private Sub ScriptControl1_Error()
MsgBox ScriptControl1.Error.Text & vbCrLf & “Error: “ & _
   ScriptControl1.Error.Description
ScriptControl1.Error.Clear
End Sub

© 1998 SYBEX Inc. All rights reserved.