How should I handle an error (raised in a class method) outside of a class in VB6?
I would like to create an object of a VB6 class and call a method on that object. If an error occurs in
that method I would like to be able to raise an error in the method and catch it outside the class, in the
routine which I called the method.
The class for example may contain:
Const cmlngMYERROR As Long = vbObjectError + 1001
Public Sub MyMethod()
...
Err.Raise cmlngMYERROR, Err.Source, Err.Description
End Sub
The calling routine may contain:
Private Sub MyCallingRoutine()
Dim objMyObject As ClassName
On Error GoTo ErrorHandler
Set objMyObject = New ClassName
objMyObject.MyMethod
Exit Sub
ErrorHandler:
If Err.Number=clngMYERROR Then
...
End If
End Sub
The problem I have is that you cannot define public constants at the top of a class. Therefore, you cannot
check the error number using the defined constant in the c开发者_如何学运维alling routines error handler. What is the best
practise to check the error code in the calling routine? Am I using error handling correctly in this
example (or on the right track at least)?
Thanks in advance.
There is a clever (ugly?) trick to emulate a public constant: use a public Enum instead
Public Enum PseudoConst
cmlngMYERROR = vbObjectError + 1001
End Enum
精彩评论