-
How do I respond to an error in my calling module when the error was generated with my object?
This is what I have:
Calling Module
Code:
Private sp As SwitchPack_Class
Public Sub Cal_switchpack(ByVal sp_config As Byte)
On Error GoTo errhandler
'... code
Set sp = New SwitchPack_Class
sp.IR = 512
'... other code
exit sub
errhandler:
msgbox "Handled error" ....
End sub
Class
Code:
Public Property Get IR() As Integer
On Error GoTo errhandler
IR = GetButton(12, RAW)
Exit Property
errhandler:
err.Raise err.Number, err.Source, err.Description ' pass error back to calling module
End Property
Private Function GetButton(ByVal button, ByVal mode As Byte) As Integer
'... other code....
err.Raise 40001, "Switch Pack Object", "Timeout reading SwitchPack button"
'... more code
end function
At the moment the error gets passed back to the property get function but I cant seem to pass it back to the Cal_Swithpack module. I dont want to use a
Code:
if abort=false then sp.ir=12
as this would make my nice neat function into a massive messy one. Any ideas?
Cheers
-
If you put the Class in a seperate project then you can trap the errors in the calling module, choose AddProject from the file menu and make it an activeX dll, then add it as a refrence to your original project.
-
I don't know if this helps you or not, but I think what I use for error hadling in a Class is pretty standard.
Code:
Public Property Get IsReplacementCPPM() As Boolean
On Error GoTo ErrorRoutine
IsReplacementCPPM = m_bIsReplacementCPPM
Exit Property
ErrorRoutine:
ClassError "IsReplacementCPPM Property Get"
End Property
Private Sub ClassError(sRoutine As String)
Dim lError As Long
lError = Err.Number
Err.Clear
'Send the error back to the main program
Err.Raise lError, TypeName(Me) & "::" & sRoutine
End Sub
-
Martin, I can get this to work but it does`nt use my module (calling function handler) it pops up the VB msgbox. Anyideas?
-
Code:
On Error GoTo ErrorRoutine
If MyClass.IsReplacementCPPM Then
' blah, blah, blah
End If
Exit Sub
ErrorRoutine:
CallMyErrorHandlingRoutine
End Sub
-
VB always breaks on errors in class modules by default. To change this behaviour click on Tools|Options and the General tab. In the Error Trapping frame click the "Break on Unhandled Errors" options.
Best regards
-
I dont want to directly call the module handling function. I want to be invoked by the error eg.
Code:
Private sub MyModule()
'...code
On error goto errhandler
'...code
sp.ir=12 ' call to my object
exit sub
errhandler:
Msgbox "Something funny is going on in the Switch Pack object"
End Sub
I want it to work this way because I am making lots of calls to my object and dont want to have to check for an error every time I use a get or let property.
-
Ta chaps, fixed it now (dam menu options!)
Cheers
Rick