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

To use Script.OnError error handling, write an error handler function, and set the Script.OnError or APGScript.OnError property to the function.

The Error Handler Function

The signature of the error handling function is:

Visual Basic:

Sub funcHandler (ByVal oAPGError As APGenLib.IAPGError)

Visual J++:

void funcHandler(Object oAPGError)

VBScript:

Sub funcHandler(oAPGError)

JScript:

function funcHandler(oAPGError)

The error handler function is passed an APGError object.

The function should set oAPGError.Handled to true if the error is completely handled.  This prevents subsequent error handlers from being called.  If oAPGError.Handled is not set to true, other error handlers, including the default handler, will be called.  See the topic Error Handling for more information on the error handler chain.

Attaching the Error Handler

The error handler function can be attached by setting the Script.OnError or APGScript.OnError property.  Script.OnError can be used within an APG script, and APGScript.OnErroris used externally to attach an error handler to an APGScript object.

To attach an error handler in VBScript, use the VBScript GetRef() function:

Script.OnError = GetRef("ErrorHandler")

To attach an error handler in JScript, just set the OnError property to the function:

Script.OnError = ErrorHandler;

Examples

This VBScript snippet uses APGScript.OnError to connect an external error handler to an APGScript:

Sub ErrorHandler(oAPGError)
    ...
End Sub

' Open the APG script
Dim oAPGen, oScript
Set oAPGen = CreateObject("APGen")
Set oScript = oAPGen.OpenScript("default.apg")

' Attach the error handler
oScript.OnError = GetRef("ErrorHandler")

' Run the script
oScript.Run

This APG script uses Script.OnError to connect an internal error handler.  The example primarily uses JScript, but VBScript is used to display a MessageBox.

<%# @LANGUAGE = JScript #%>
<SCRIPT runat="APGen" language="VBScript">


Function YNBox(sPrompt, sTitle)
     retval = MsgBox(sPrompt, vbYesNo, sTitle)
     If (retval = vbYes) Then
          YNBox = True
     Else
          YNBox = False
     End If    
End Function

</SCRIPT>
<%#

function ErrorHandler(oAPGError)
     {
     var bAbort = YNBox(oAPGError.Description + "\n\nAbort the script?",
                                   oAPGError.Source);
     if (bAbort)
          { // User chose yes: Abort the script
          oAPGError.Handled = true;
          Script.Abort();
          }

     // If the user chose no, the default error handler will run
     }
    

function Main()
     {
     // Set the script error handler
     Script.OnError = ErrorHandler;

     // Cause an error
     Output.MethodNotPresent();
     }

#%>

See APG Script Status Reporting in ASP for an ASP example using APGScript.OnError error handling.