How to raise error in class method such that calling code will be notified of error? (1 Viewer)

mdlueck

Sr. Application Developer
Local time
Yesterday, 19:17
Joined
Jun 23, 2011
Messages
2,631
I am seeking some relief from having to specifically check for errors in numerous calls to a class object. What I am trying to avoid having to do is wrap each call in an if statement to see if the call to the object worked successfully.

Is there some way that I can handle the error locally where the error took place, and propagate the error back to the parent so that it in turn goes to its error handler and does not plow onward with further normal LOC's?

Example, I simply want to be able to code like this:

Code:
  'Add the data to the email file
  ObjTxtFileUtils.FileWrite (vbCrLf & "Project: " & ObjProjectsTbl.title)
  ObjTxtFileUtils.FileWrite ("Product: " & ObjProductsTbl.title)
  ObjTxtFileUtils.FileWrite (vbCrLf & "Part Number: " & ObjPartsTbl.partnumber)
  ObjTxtFileUtils.FileWrite ("Part Description: " & ObjPartsTbl.title)
  ObjTxtFileUtils.FileWrite (vbCrLf & "Completed By: " & Me.complbycontactname)
  ObjTxtFileUtils.FileWrite ("Completed Date: " & Me.compldate)
  ObjTxtFileUtils.FileWrite ("Vendor: " & Me.vendortitle)
  ObjTxtFileUtils.FileWrite ("FA Type: " & Me.fatypetitle)
  ObjTxtFileUtils.FileWrite ("PQP: " & Me.pqptypetitle)
  ObjTxtFileUtils.FileWrite ("Gage Plan: " & Me.gptitle)
  ObjTxtFileUtils.FileWrite ("Comments: " & Me.comments)
However, if one of those FileWrite calls should fail, I would like that error to be propagated to this code, and for this code to immediately jump to its "On Error GoTo" handler.

Is there a way I can code the FileWrite class method that it would raise to the code using it that there was an error?

Or must I wrap each and every FileWrite call with an if statement? TIA!
 

MarkK

bit cruncher
Local time
Yesterday, 16:17
Joined
Mar 17, 2004
Messages
8,186
If an unhandled error occurs in a subroutine, control is passed back to the calling routine, so just enable an error handler in the code you posted.
Code:
Sub WriteToFile
On Error GoTo Handler
[COLOR="Green"]  'Add the data to the email file[/COLOR]
  ObjTxtFileUtils.FileWrite (vbCrLf & "Project: " & ObjProjectsTbl.title)
  ObjTxtFileUtils.FileWrite ("Product: " & ObjProductsTbl.title)
  ObjTxtFileUtils.FileWrite (vbCrLf & "Part Number: " & ObjPartsTbl.partnumber)
  ObjTxtFileUtils.FileWrite ("Part Description: " & ObjPartsTbl.title)
  ObjTxtFileUtils.FileWrite (vbCrLf & "Completed By: " & Me.complbycontactname)
  ObjTxtFileUtils.FileWrite ("Completed Date: " & Me.compldate)
  ObjTxtFileUtils.FileWrite ("Vendor: " & Me.vendortitle)
  ObjTxtFileUtils.FileWrite ("FA Type: " & Me.fatypetitle)
  ObjTxtFileUtils.FileWrite ("PQP: " & Me.pqptypetitle)
  ObjTxtFileUtils.FileWrite ("Gage Plan: " & Me.gptitle)
  ObjTxtFileUtils.FileWrite ("Comments: " & Me.comments)
  exit sub
handler:
[COLOR="Green"]  'handle the error here[/COLOR]
  msgbox err & " " & err.description
End Sub
Also, while debugging classes, be aware that you can set the debugger to break in your class, which it doesn't do by default. Check out the settings in Tools Menu->Options Menu->General Tab->Error Trapping Section. Check 'Break In Class Module.'
 

mdlueck

Sr. Application Developer
Local time
Yesterday, 19:17
Joined
Jun 23, 2011
Messages
2,631
I do have an error handler in the code which uses the ObjTxtFileUtils object already.

The ObjTxtFileUtils methods themself handle errors as well.

My point was I did not want to have to specifically check return codes from the ObjTxtFileUtils object, and to instead just have the general error handler pick up on the fact that an error occurred inside the ObjTxtFileUtils object.

Is there some better way to indicate to the code using the ObjTxtFileUtils object that something went wrong inside the ObjTxtFileUtils object / cause said code to also jump to its error handler?

Or must I do if/then checking of EACH ObjTxtFileUtils execution?
 

MarkK

bit cruncher
Local time
Yesterday, 16:17
Joined
Mar 17, 2004
Messages
8,186
So your subroutines handle errors? If you want the calling routine to handle the errors disable the handlers in the subroutines. Or you can raise custom errors, so consider...
Code:
Public Sub Test321()
  On Error GoTo handler
    Dim b As Byte

    MsgBox 1 / 0 [COLOR="Green"]'will raise division by zero, which we handle locally[/COLOR]
    b = -1   [COLOR="Green"]'will raise overflow, which we re-raise for calling sub to handle[/COLOR]

    Exit Sub

handler:
    Select Case err
      Case 11  [COLOR="Green"]'division by zero[/COLOR]
[COLOR="Green"]        'handle locally[/COLOR]
        Resume Next
      Case Else
[COLOR="Green"]        'we re-raise all errors except 11[/COLOR]
        err.Raise err
    End Select
End Sub
Does that help? In this way you can selectively determine which error your subroutines will raise. You can also selectively raise your own custom errors, like...
Code:
Sub Testing123()
[COLOR="Green"]'   This routine will raise a custom error some of the time[/COLOR]
    Dim tmp As Integer
    tmp = right(Format(Time(), "ss"), 1)
    If tmp < 3 Then
        err.Raise -1, "Testing123()", "This is my custom error description"
    Else
        MsgBox "No error this time"
    End If
    
End Sub
So that raises an error with a custom number, source and description, but only some of the time.
 

mdlueck

Sr. Application Developer
Local time
Yesterday, 19:17
Joined
Jun 23, 2011
Messages
2,631
I found a way of coding an object method that the method itself may handle the original error, yet will raise an error to be handled by the caller's error handler.

Code:
Public Function FileWrite(ByVal varThisData As Variant) As Boolean
  On Error GoTo Err_FileWrite

  Print #Me.FileNumber, varThisData
'LOC for testing the error handler...
'GoTo Err_FileWrite

  'Good return code
  FileWrite = True

Exit_FileWrite:
  Exit Function

Err_FileWrite:
  Call errorhandler_MsgBox("Class: clsObjTxtFileUtils, Function: FileWrite()")
[COLOR=Blue]  'Disable further error handling, so that the code which is using this object will handle the error
  On Error GoTo 0[/COLOR]
  FileWrite = False
[COLOR=Blue]  'Raise the error to the caller program
  Err.Raise -1, "Class: clsObjTxtFileUtils, Function: FileWrite()"[/COLOR]
  Resume Exit_FileWrite

End Function
Thus I may not specifically test the return code of each object call, just like I wanted to. :cool:
 

Users who are viewing this thread

Top Bottom