I asked a question elsewhere on Stack Exchange and was given an answer by multiple people that checking for errors in-line was not a good practice. I have been using an
on error resume next ' do something on error goto 0
block structure for years as kind of an improvised
catch construct for VBA.
Here is a simple example of some actual code:
I need to check if an object has been passed before I attempt to access the properties of the object. Rather than having the same Error handling label in each subroutine that accesses that object, I've opted to do my error check in a function that returns a Boolean value telling me whether or not the object has been initialized.
I want to know if this is acceptable practice, or if there is a better way this should be handled.
This is NOT the complete code. I just took the relevant props/methods for the question and added them below. The main program allows data sharing, updating, and communication between three different systems. This example is taken from a class object which bridges communication between a corporate intranet website (through ASP) and a Solidworks model object, whose information is made available using the
Option Explicit 'Declare module level constants Private Const errNoMyModel As Long = -999 'Declare module level variables Private pMyMod As Class_MyModel 'PROPERTY that holds an instance of custom class MyModel which provides all the model info from solidworks Public Property Get MyMODEL() As Class_MyModel Set MyMODEL = pMyMod End Property Public Property Let MyMODEL(object As Class_MyModel) Set pMyMod = object End Property 'METHOD which can be called to submit a drawing Public Sub SubmitDrawing() DoSubmittal End Sub Private Sub DoSubmittal() 'This procedure updates the intranet site with info from the mymodel object _ it will create a new record if none exists for the given rev, or update the _ record if one already exists 'Check first that the mymodel object property has been set If Not MyModelExists Then Exit Sub 'Nothing can be done if we don't have a mymodel object to work with 'Declare variables Dim strURL As String Dim blnExists As Boolean 'Call the procedure that deals with existing records. blnExists = HandleExisting(False) 'The procedure above returns a true value if a record already existed for the current revision, so if it returns false, we need to add a new record for the current revision If Not blnExists Then 'Construct the URL that will add a new record for the current revision strURL = GetURL(aNew) 'Call the procedure to execute the URL. Print the return text to the debug window so it can be reviewed if necessary. Debug.Print MyMODEL.PARTNO & " " & MyMODEL.REVISION & " CREATE NEW RECORD ATTEMPT RETURNED : " & vbCrLf & DoASP(strURL, False) 'Calls the procedure to exectue the URL and prints the ASP return message to the debug window. End If 'Activate the current the record for the current revision HandleExisting (True) End Sub Private Function MyModelExists() As Boolean 'This function simply checks that the calling procedure has successfully passed a Class_MyModel object to work with 'Declare variables Dim tempModel As Class_MyModel 'Attempt to set an object to reference the mymodel object instance On Error Resume Next Err.Clear Set tempModel = MyMODEL On Error GoTo 0 'Check if the attempt was successful, if not, then no mymodel object exists. _ Return the result to the calling procedure If tempModel Is Nothing Then MyModelExists = False ErrorMsg (errNoMyModel) Else MyModelExists = True End If If Err.Number > 0 Then ErrorMsg 'Cleanup objects before ending the procedure because don't entirely trust VBA's garbage collection Set tempModel = Nothing End Function Private Sub ErrorMsg(Optional ByRef ErrNum As Long) 'This is a simple error message handling procedure 'Choose what do do based on the error that occurred Select Case ErrNum Case errNoMyModel 'This is a constant delcared at the module level that is used to identify an error where the mymodel property has not been set before attempting to call the procedure to update the entry MsgBox "In order to proceed, a MyModel object containing a Solidworks model must be passed to this object. " & _ "Please check that you have selected a valid Solidworks object (.sldprt, .slddrw, .sldasm). " & _ "If you continue to get this message when a valid object is selected, please contact the software developer " & _ "to resolve this problem.", vbOKOnly, "ERROR: Object not assigned" Case Else 'For all other erros, just inform the user of what happened MsgBox "ERROR! " & vbCrLf & "Error type: " & Err.DESCRIPTION & vbCrLf & "Error number: " & Err.Number & vbCrLf & "Error source: " & Err.Source, vbOKOnly, "ERROR" End Select End Sub