APGen Documentation Previous Topic: Using Script.OnError Next Topic: Logging in APGen Parent Topic: Error Handling    Error Handling
Script Error Events
See Also:

Script error events are COM events fired by the Script and APGScript objects when errors occur.

Many tools on the Windows platform can handle COM Automation and COM events.  Script error events are a way for these tools to take part in the error handler chain.

The signature for Script error event handlers is the same as the signature for Script.OnError error handler functions

Visual Basic Example

This Visual Basic example runs an APG script, and handles errors in that script by displaying a MsgBox and asking if the script should be aborted.  As mentioned in Executing APG Script in Visual Basic, the Active Page Generator 2.1 Type Library must be added to the references for the VB project.


Private WithEvents oScript As APGenLib.APGScript

Private Sub Form_Load()

    Dim oAPGen As APGen
    
    Set oAPGen = New APGen
    Set oScript = oAPGen .OpenScript("c:\apgs\script.apg")

    oScript.Run

End Sub

Private Sub oScript_Error(ByVal oAPGError As APGenLib.IAPGError)

    If oAPGError.Severity <> apgSeverityFatal Then
        Exit Sub
    End If
    
    Dim mbRet
    mbRet = MsgBox(oAPGError.Description & vbCrLf & _
          "Stop Error Handling Chain?", vbYesNo, oAPGError.Source)

    If mbRet = vbYes Then
        oAPGError.Handled = True
        oAPGError.Script.Abort
    End If

End Sub

JScript Example

This example is similar to the Visual Basic example just shown - it runs another APG script, and it handles any Error events fired by the script.  The event is connected to the event handler using Script.ConnectEvents().  Since JScript doesn't have a MsgBox function, it handles the error by logging the error to the NT Event Log, and aborting the script. 

<%# @Language="JScript" #%>
<%#

function Main()
     {
     // Open the script
     var oAPGen = Script.CreateObject("APGen");
     var oScript = oAPGen.OpenScript("c:\\temp\\apgs\\hello.apg");

     // Connect the error handler to oScript
     Script.ConnectEvents(oScript, "oScript_");
     
     // Run the script - if any errors occur, they'll
     //     be caught by oScript_Error
     oScript.Run();
    
     // Disconnect event handler
     Script.DisconnectEvents(oScript);
     }


// Error event handler for oScript
function oScript_Error(oAPGError)
     {
     if (oAPGError.Severity != apgSeverityFatal)
          return;

     // Send report of fatal error to the NT Event Log
     var sError = "Fatal error occurred in script '" +
                         oAPGError.Script.Path + "'\r\n\r\n";
     Log.LogEvent(sError + oAPGError.Description, apgSeverityFatal, 
                    oAPGError.Number);
    
     // Mark error as handled, so default error handler doesn't run
     oAPGError.Handled = true;
    
     // Abort the script, so oScript.Run() doesn't raise an error
     oAPGError.Script.Abort();
     }

#%>