Macros/ScriptForge/HandlingErrorsInEvents
Appearance
< Macros | ScriptForge
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
How to handle errors occurring in events
Authored by Jean-Pierre Ledure.
Three scenarios are shown:
- Doing nothing
- Using the standard Basic error handling
- Using ScriptForge facilities
The error is simulated with a simple division by zero.
This snippet is BASIC-only.

How to run it in BASIC
Create a new document.
- Open the Basic IDE
- Select the first available blank module
- Copy and paste the Basic code
- Verify lines 14-16
- Run Main()
REM SCRIPTFORGE WIKI EXAMPLE
REM How to manage errors in scripts triggered by events ?
REM Minimal required version: LibreOffice 7.6
REM Used services
REM SFDialogs.Dialog, SFDialogs.DialogControl, ScriptForge.Exception
Option Explicit
Public dialog As Object ' The SFDialogs.Dialog object
'*******************************************************************
'*** Adjust module name if not "Module1'
'*******************************************************************
Const errnomgt = "vnd.sun.star.script:Standard.Module1.ErrMgtNone?language=Basic&location=document"
Const errbasicmgt = "vnd.sun.star.script:Standard.Module1.ErrMgtBasic?language=Basic&location=document"
Const errsfmgt = "vnd.sun.star.script:Standard.Module1.ErrMgt?language=Basic&location=document"
'*******************************************************************
'* Run the demo below
'*******************************************************************
Sub Main
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
SetupDialog()
RunDialog()
dialog.Terminate()
End Sub
'*******************************************************************
'* Specific demo code
'*******************************************************************
Sub RunDialog()
Dim response As Integer
Dim exception As Object : Set exception = CreateScriptService("Exception")
response = dialog.Execute()
Select Case response
Case 0 ' Cancel - Window close button
Case 1 ' OK - Close button
Case 2 ' Something went wrong
' Housekeeping
exception.Console(False)
End Select
End Sub
'*******************************************************************
Sub ErrMgtNone(Optional event As Variant)
REM An error message is raised on the Execute() statement (above) but location is unknown
REM The dialog is NOT terminated
Dim div As Integer
div = 0
MsgBox 5 / div
Exit Sub
End Sub
'*******************************************************************
Sub ErrMgtBasic(Optional event As Variant)
REM Usual Basic error handling, error code and location are known
Dim div As Integer
Dim exc As Object : Set exc = CreateScriptService("Exception")
On Local Error GoTo Finally
div = 0
MsgBox 5 / div
Exit Sub
Finally:
MsgBox Err & "/" & Erl & "/" & Error$
On Local Error GoTo 0 ' Avoid the propagation of the error
End Sub
'*******************************************************************
Sub ErrMgt(Optional event As Variant)
REM ScriptForge error handling with debugging info
Dim div As Integer ' Errorneous divider
Dim exc As Object : Set exc = CreateScriptService("Exception")
On Local Error GoTo Finally
div = 0
MsgBox 5 / div
Exit Sub
Finally:
exc.RaiseWarning() ' To not stop, use RaiseWarning(). To force a stop, use Raise()
exc.Clear() ' Avoid the propagation of the error
exc.DebugPrint "Error in Sub ErrMgt - root cause to be investigated"
exc.DebugPrint "div =", div ' Note the actual values of key variables
dialog.EndExecute(2) ' Give feedback to caller
End Sub
'*******************************************************************
'* May be defined with the Basic IDE
'*******************************************************************
Sub SetupDialog()
' Build from scratch the example dialog and its controls
Dim control As Object ' A SFDialogs.DialogControl object
Set dialog = CreateScriptService("NewDialog", "ErrMgt", Array(170, 135, 140, 100))
dialog.Caption = "Error management"
With dialog
' The close button
Set control = .CreateButton("CloseButton", place := Array(110, 85, 30, 10), push := "OK")
control.Caption = "Close"
' The buttons simulating errors
Set control = .CreateFixedText("NoErrorMgt", place := Array(20, 10, 100, 10), verticalalign := "middle")
control.Caption = "No error management"
Set control = .CreateButton("TriggerErrorNone", place := Array(35, 20, 70, 10))
control.Caption = "Trigger an error"
control.OnActionPerformed = errnomgt
Set control = .CloneControl("NoErrorMgt", "BasErrMgt", left := 20, top := 35)
control.Caption = "Basic error management"
Set control = .CloneControl("TriggerErrorNone", "TriggerErrBas", left := 35, top := 45)
control.OnActionPerformed = errbasicmgt
Set control = .CloneControl("NoErrorMgt", "ErrMgt", left := 20, top := 60)
control.Caption = "Customized error management"
Set control = .CloneControl("TriggerErrorNone", "TriggerErr", left := 35, top := 70)
control.OnActionPerformed = errsfmgt
End With
End Sub