DevelHelper
asked on
Question on Err.Raise
Attached are two test files: One is COM dll file; One is client exe file. I made a simple dll method "Test" call from client with empty SSN value passing. Why can't the client handle the Err.Raise (not be hit if put a breakpoint at "MsgBox Err.Description" at client)?
'************************* *****
' comTest - Class1.cls
'************************* *****
Public Function Test(ByVal SSN As String) As String
On Error GoTo errh
If Len(SSN) = 0 Then
Err.Raise vbError + 1, , "Please input SSN" ' create run time error
Exit Function
End If
Test = "Successful"
Exit Function
errh:
Test = Err.Description
End Function
'************************* *******
' clientTest - Form1
'************************* *******
Private Sub cmdTest_Click()
Dim oTest As Object
On Error GoTo errh
Set oTest = CreateObject("comTest.Clas s1")
MsgBox oTest.Test(txtSSN)
Exit Sub
errh:
MsgBox Err.Description ' set breakpoint here
End Sub
'*************************
' comTest - Class1.cls
'*************************
Public Function Test(ByVal SSN As String) As String
On Error GoTo errh
If Len(SSN) = 0 Then
Err.Raise vbError + 1, , "Please input SSN" ' create run time error
Exit Function
End If
Test = "Successful"
Exit Function
errh:
Test = Err.Description
End Function
'*************************
' clientTest - Form1
'*************************
Private Sub cmdTest_Click()
Dim oTest As Object
On Error GoTo errh
Set oTest = CreateObject("comTest.Clas
MsgBox oTest.Test(txtSSN)
Exit Sub
errh:
MsgBox Err.Description ' set breakpoint here
End Sub
ASKER
Thank you for your help. But this just a simple code. Actually, there are two different errors should be handled. One error is from database connection; another is from VB code. "On Error GOTO errh" is used to handle error from VB code.
Handle the error in the dll and then either raise an error or fire an event in that error handler. That'll pass that error back to the client and you can process it there. You'll have to use the with events keyword in your dll declaration to use the event method, though
ASKER
Could you please tell how to make change on the code?
In your dll, put:
Public Event ErrorEvent(iMsg as Long, sMsg as String)
In your error handler, put this:
errh:
RaiseEvent ErrorEvent(err.Number, err.Description)
In your form put:
dim WithEvents oTest as comTest.Class1
set oTest = new comTest.Class1
and add the method
private sub oTest_ErrorEvent(iMsg as Long, sMsg as String)
.... do whatever here
end sub
Public Event ErrorEvent(iMsg as Long, sMsg as String)
In your error handler, put this:
errh:
RaiseEvent ErrorEvent(err.Number, err.Description)
In your form put:
dim WithEvents oTest as comTest.Class1
set oTest = new comTest.Class1
and add the method
private sub oTest_ErrorEvent(iMsg as Long, sMsg as String)
.... do whatever here
end sub
You wrote:
"Err.Raise vbError + 1, , "Please input SSN" "
For COM, it needs to be VBObjectError + 1.
"Err.Raise vbError + 1, , "Please input SSN" "
For COM, it needs to be VBObjectError + 1.
It is simple: BEFORE *each* Err.Raise statement add one line:
On Error GoTo 0
Err.Raise ...
The Err.Raise will also work correctly without that line, if you don't have error handler in your function.
But, it is good idea to ignore that fact and to add "On Error GoTo 0" statements - this will allow later changes to error handling in your function.
On Error GoTo 0
Err.Raise ...
The Err.Raise will also work correctly without that line, if you don't have error handler in your function.
But, it is good idea to ignore that fact and to add "On Error GoTo 0" statements - this will allow later changes to error handling in your function.
- you can also remove "Exit Function" - it is never executed, since Err.Raise exits function and returns code execution to the caller.
Public Function Test(ByVal SSN As String) As String
On Error GoTo errh
If Len(SSN) = 0 Then
On Error GoTo 0 ' turn off error handling
Err.Raise vbError + 1, , "Please input SSN" ' create run time error
End If
Test = "Successful"
Exit Function
errh:
Test = Err.Description
End Function
Public Function Test(ByVal SSN As String) As String
On Error GoTo errh
If Len(SSN) = 0 Then
On Error GoTo 0 ' turn off error handling
Err.Raise vbError + 1, , "Please input SSN" ' create run time error
End If
Test = "Successful"
Exit Function
errh:
Test = Err.Description
End Function
ASKER
Thanks! everyone.
Finally, I find it works correctly if I register the com dll in my computer, and then run the client project. But I don't know why it can't works correctly if run the com within VB project group.
Finally, I find it works correctly if I register the com dll in my computer, and then run the client project. But I don't know why it can't works correctly if run the com within VB project group.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your Com control is handling the error, but you want the client to handle it right?
When you're raising the error in Test, it goes to Test=Err.description and the client never even knows an error occured.