Objecte Err [VBA]

Use VBA Err object to raise or handle runtime errors.

Err 茅s un objecte global integrat del VBA que permet:

warning

Aquesta constant, funci贸 o objecte s'activa mitjan莽ant l'expressi贸 Option VBASupport 1, col路locada abans del codi executable del programa en un m貌dul.


The VBA Err object has the following properties and methods:

Propietats


         Err.Description As String
      

La propietat Descripci贸 d贸na la naturalesa de l'error. Descripci贸 detalla les diverses raons que poden ser la causa de l'error. Idealment proporciona el curs m煤ltiple d'accions per ajudar a resoldre el problema i evitar la seva reaparici贸. L'脿lies b脿sic 茅s la funci贸 Error per a errors predefinits de %{PRODUCTNAME}.


         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 indicates the name of the routine that produces the error. Source is an option for user-defined errors.

M猫todes


         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.

Par脿metres

Nombre Un codi d'error definit per l'usuari o predefinit a pujar.

note

Error l'interval de codi 0-2000 est脿 reservat per a un percentatge de LibreOffice Basic. Els errors definits per l'usuari poden comen莽ar des de valors m茅s alts per tal d'evitar la col路lisi贸 amb el desenvolupament futur del LibreOffice Basic.


Source El nom de la rutina que aixeca l'error. Es recomana un nom en forma de 芦myLibrary.myModule.myProc禄.

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.

Exemple:


         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
      

Exception ClassModule

tip

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
      

Exemple


         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")
             ' el vostre codi va aqu铆鈥
         finally:
             Exit Sub
         catch:
             errTitle = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source
             MsgBox Exc.Description, MB_ICONSTOP, errTitle
             Resume finally
         End Sub
      
note

The Error statement or an Exception-like class module can be used interchangeably, while the latter adds extra features.


Ens cal la vostra ajuda!