Err Object [VBA]
Bruk VBA-objektet Err for Ă„ heve eller handsama kĂžyretidsfeil.
Err er eit innebygd VBA-objekt som tillet:
for Ă„ lĂžfte fĂžrehandsdefinerte feil i Basic
for Ă„ fjerna brukardefinerte unnatak
Ä setja namn pÄ rutinen som var Ärsaka til feilen
Ă„ skildra feilen og mogleg lĂžysing
VBA Err-objektet har desse eigenskane og metodane:
Eigenskapar
Err.Description As String
Eigenskapen Description viser fejlens natur. Det vert gjeve ei detaljert forklaring pÄ kva som kan vera Ärsaka til feilen. Ideelt sett gjev han fleire forslag til korleis feilen kan rettast og hindra at han oppstÄr igjen. Aliaset er Basicfunksjonen Error for LibreOffice fÞrehandsdefinerte feil.
Err.Number As Long
Dette er feilkoden knytt til feilen. Standardeigenskapane for objektet Err er Number. Aliaset er % PRODUCTNAME Basic-funksjonen Err.
Err.Source As String
Source viser namnet pÄ rutinen som var Ärsak til feilen. Source er ei innstilling for brukardefinerte feil.
Metodar
Err.Clear()
Nullstiller skildringa Erl, nummer og kjeldeeigenskapane for den gjeldande feilen. Aliaset er uttrykket LibreOffice Basic Resume.
Err.Raise(Number As Long, Optional source As String, Optional description As String)
Kastar brukardefinerte feil eller fĂžrehandsdefinerte feil. Aliaset er LibreOffice Basic Error-udtryk
Parameterar
Number A user-defined or predefined error code to be raised.
Error code range 0-2000 is reserved for LibreOffice Basic. User-defined errors may start from higher values in order to prevent collision with LibreOffice Basic future developments.
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 intented 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
Exception ClassModule
A short ClassModule, that wraps VBA Err object, can distribute Err properties and methods for standard LibreOffice Basic modules.
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
Example
Function Exc As Object
Exc = New Exception
End Function
Sub aRoutine
try:
On Local Error GoTo catch:
Exc.Raise(4096, "myLib.myModule.aRoutine", _
"Any multi-line description for this user-defined exception")
' your code goes here âŠ
finally:
Exit Sub
catch:
errTitle = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source
MsgBox Exc.Description, MB_ICONSTOP, errTitle
Resume finally
End Sub
The Error statement or an Exception-like class module can be used interchangeably, while the latter adds extra features.