Use VBA Err object to raise or handle runtime errors.Err is a built-in VBA global object that allows:to raise predefined Basic errorsto throw user-defined exceptionsto name the routine originating the errorto describe the error and possible solutionsVBA Err object has the following properties and methods:
Properties
Err.Description As StringDescription 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 %PRODUCTNAME predefined errors.Err.Number As LongThis the error code associated with the error. Err object default property is Number. Its alias is %PRODUCTNAME Basic Err function.Err.Source As StringSource indicates the name of the routine that produces the error. Source is an option for user-defined errors.
Methods
Err.Clear()Resets description, Erl, number and source properties of current error. Its alias is %PRODUCTNAME 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 %PRODUCTNAME Basic Error statement.
Parameters
Number A user-defined or predefined error code to be raised.Error code range 0-2000 is reserved for %PRODUCTNAME Basic. User-defined errors may start from higher values in order to prevent collision with %PRODUCTNAME 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 1Sub 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 SubAlertAndExecNext: errTitle = "Error "& Err &" at line "& Erl &" in "& Err.Source MsgBox Err.Description, MB_ICONEXCLAMATION, errTitle Resume NextEnd Sub
Exception ClassModule
A short ClassModule, that wraps VBA Err object, can distribute Err properties and methods for standard %PRODUCTNAME Basic modules.Option ClassModuleOption VBASupport 1Public Property Get Description As String Description = Err.DescriptionEnd PropertyPublic Property Get Number As Long Number = Err.NumberEnd PropertyPublic Property Get Source As String Source = Err.SourceEnd PropertyPublic Sub Clear Err.ClearEnd SubPublic Sub Raise( number As Long, Optional Source As String, Optional Description As String) Err.Raise number, Source, DescriptionEnd Sub
Example
Function Exc As Object Exc = New ExceptionEnd FunctionSub aRoutinetry: 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 Subcatch: errTitle = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source MsgBox Exc.Description, MB_ICONSTOP, errTitle Resume finallyEnd SubThe Error statement or an Exception-like class module can be used interchangeably, while the latter adds extra features.