Hi Kathy--
There is no simple way to handle all of the possible errors. Error coding in fact can take up 1/3 or more of your code. Below is the template I use for my procedures. Note that errors are detected in the main body of the procedure and then sent to the error handler at the end. If the procedure does not have an error handler, it will bubble up the call tree until it hits the first routine that does have an error handler. You will have to decide for yourself what happens when the error hits the handler. In my case, I use the "Report" sub in a global instance of SystemError class, an error handler class that will log the error, send a copy to the debug window and report it to a message box. If I only wanted to log and send it to debug, I would call SystemError.post. You do not need such a complicated mechanism. You may just want to use debug.print and/or message box in the error handling section. I use a class to provide uniform processing of all errors and to allow for much more complicated behavior.
Once you get into the error handler, you have to decide what to do next. Do you abort (End) or do you raise a new error to bubble the error up (SystemError bubbles it up). Remember that in general, error processing should be rare and therefore does not have to be efficient.
Hope this helps.
Barry
ps CLASS_NAME and Class_ProcedurePath are declared once in every class and bas file (though in a base file CLASS_NAME would be called BAS_NAME.
''================================================ =============================
Private Const CLASS_NAME = "PutClassNameHere"
''================================================ =============================
Private Function Class_ProcedurePath(ByVal ProcedureName As String) As String
Class_ProcedurePath = CLASS_NAME & ">" & ProcedureName
End Function
''================================================ =============================
Private Sub dummy()
'------------------------------------------------------------------------------
Const PROCEDURE_NAME = "dummy"
Const BAD_xxx = 32601
On Error GoTo ErrorHandler
'------------------------------------------------------------------------------
Dim variable As Variant
If Not variable Then
Err.Raise BAD_xxx, , "Bad Enum <" & variable & ">"
End If
Exit Sub' MUST BE HERE, OTHERWISE EXECUTION WILL CONTINUE INTO HANDLER
'------------------------------------------------------------------------------
ErrorHandler:
Select Case Err.number
Case BAD_xxx
SystemError.Report Err.number, Class_ProcedurePath(PROCEDURE_NAME), Err.Description
Case Else
SystemError.Report Err.number, Class_ProcedurePath(PROCEDURE_NAME), Err.Description
End Select
Exit Sub
'------------------------------------------------------------------------------
End Sub
|