Err ojektas [VBA]
Naudokite objektą VBA Err, kad rastumėte arba apdorotumėte vykdymo klaidas.
Err įterpta į VBA globalųjį objektą, kuris leidžia:
rasti iš anksto apibrėžtas „Basic“ klaidas
atsisakyti vartotojo apibrėžtų išimčių
įvardyti paprogramą, kurioje atsirado klaida
apibūdinti klaidą ir galimus sprendimo būdus
VBA Err object has the following properties and methods:
Savybės
Err.Description As String
Description property gives the nature of the error. It details the various reasons that may cause the error. Ideally, it provides the multiple course of actions to help solve the issue and prevent its reoccurrence. Its alias is Basic Error function for LibreOffice predefined errors.
Err.Number As Long
This the error code associated with the error. Err object default property is Number. Its alias is LibreOffice Basic Err function.
Err.Source As String
Šaltinis nurodo paprogramės, kuri sukelia klaidą, pavadinimą. Šaltinis yra vartotojo apibrėžtų klaidų parinktis.
Metodai
Err.Clear()
Resets description, Erl, number and source properties of current error. Its alias is LibreOffice Basic Resume statement.
Err.Raise(Number As Long, Optional source As String, Optional description As String)
Throws user-defined errors or predefined errors. Its alias is LibreOffice Basic Error statement.
Parametrai
Number A user-defined or predefined error code to be raised.
Klaidų kodų intervalas 0–2000 yra rezervuotas programai „LibreOffice Basic“. Vartotojo apibrėžtos klaidos gali prasidėti nuo didesnių reikšmių, kad būtų išvengta nesusipratimų su būsimais „% PRODUCTNAME Basic“ išplėtimais.
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 = "Klaida "& Err &" eilutės Nr. "& Erl &" šaltinis "& Err.Source
MsgBox Err.Description, MB_ICONEXCLAMATION, errTitle
Resume Next
End Sub
ClassModule išimtis
Trumpas modulis ClassModule, kuris sulanksto VBA Err objektą, gali paskirstyti Err standartinio „LibreOffice Basic“ modulio savybes ir metodus.
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
Pavyzdys
Function Exc As Object
Exc = New Exception
End Function
Sub aRoutine
try:
On Local Error GoTo catch:
Exc.Raise(4096, "myLib.myModule.aRoutine", _
"Bet koks šios vartotojo apibrėžtos išimties kelių eilučių aprašymas")
' jūsų kodas eina čia
finally:
Exit Sub
catch:
errTitle = "Klaida "& Exc.Number &" eilutėje "& Erl &" šaltinis "& Exc.Source
MsgBox Exc.Description, MB_ICONSTOP, errTitle
Resume finally
End Sub
Error sakinį ar į išimtį panašų klasės modulį galima naudoti pakaitomis, nors pastarieji prideda papildomų funkcijų.