Hilfe fĂĽr LibreOffice 7.1
Verwenden Sie das VBA-Objekt Err, um Laufzeitfehler auszulösen oder zu behandeln.
Err ist ein integriertes globales VBA-Objekt, das Folgendes ermöglicht:
um vordefinierte Grundfehler auszulösen
um benutzerdefinierte Ausnahmen auszulösen
die Routine, die den Fehler verursacht, zu benennen
um den Fehler und mögliche Lösungen zu beschreiben
The VBA Err object has the following properties and methods:
Err.Description As String
The Description property gives the nature of the error. Description details the various reasons that may be the cause of the error. Ideally, it provides the multiple course of actions to help solve the issue and prevent its reoccurrence. The Basic alias is the Error function for LibreOffice predefined errors.
Err.Number As Long
The error code associated with the error. Err object default property is Number. The LibreOffice Basic alias is the Err function.
Err.Source As String
Source gibt den Namen der Routine an, die den Fehler erzeugt. Source ist eine Option fĂĽr benutzerdefinierte Fehler.
Err.Clear()
Resets description, Erl, number and source properties of current error. The LibreOffice Basic alias is the Resume statement.
Err.Raise(Number As Long, Optional source As String, Optional description As String)
Throws user-defined errors or predefined errors. The LibreOffice Basic alias is the Error statement.
Number: A user-defined or predefined error code to be raised.
Der Fehlercodebereich 0-2000 ist für LibreOffice Basic reserviert. Benutzerdefinierte Fehler sollten bei höheren Werten beginnen, um Kollisionen mit zukünftigen Entwicklungen von LibreOffice Basic zu vermeiden.
Source: The name of the routine raising the error. A name in the form of "myLibrary.myModule.myProc" is recommended.
Description: A description of the problem leading to stop the running process, accompanied with the various reasons that may cause it. A detailed list of the possible course of actions that may help solve the problem is recommended.
Option VBASupport 1
Sub ThrowErrors
Dim aDesc As String : aDesc = Space(80)
On Local Error GoTo AlertAndExecNext
Err.Raise(91, "ThrowErrors", Error(91))
Err.Raise 2020, Description:="This is an intended user-defined error …"
Err.Raise(4096, "Standard.Module1.ThrowErrors", aDesc)
Exit Sub
AlertAndExecNext:
errTitle = "Error "& Err &" at line "& Erl &" in "& Err.Source
MsgBox Err.Description, MB_ICONEXCLAMATION, errTitle
Resume Next
End Sub
Ein kurzes ClassModule, welches das VBA-Objekt Err umschlieĂźt, kann Eigenschaften und Methoden in Err fĂĽr Standardmodule in LibreOffice Basic verteilen.
Option ClassModule
Option VBASupport 1
Public Property Get Description As String
Description = Err.Description
End Property
Public Property Get Number As Long
Number = Err.Number
End Property
Public Property Get Source As String
Source = Err.Source
End Property
Public Sub Clear
Err.Clear
End Sub
Public Sub Raise( number As Long, Optional Source As String, Optional Description As String)
Err.Raise number, Source, Description
End Sub
Function Exc As Object
Exc = New Exception
End Function
Sub aRoutine
try:
On Local Error GoTo catch:
Exc.Raise(4096, "myLib.myModule.aRoutine", _
"Beliebige mehrzeilige Beschreibung fĂĽr diese benutzerdefinierte Ausnahme")
' Ihr Code hgehört hier hin …
finally:
Exit Sub
catch:
errTitel = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source
MsgBox Exc.Description, MB_ICONSTOP, errTitle
Resume finally
End Sub
Die Anweisung Error oder ein Exception-ähnliches Klassenmodul können austauschbar verwendet werden, während letzteres zusätzliche Funktionen hinzufügt.