|
-
Sep 22nd, 2000, 11:41 AM
#1
Thread Starter
Lively Member
I have one class and one main program which calls that class. In the class, I have trap for an error and use Err.Raise method to fire the error to the main program, but it doesn't work so far.
Any idea? Thanks
Code:
Public Function GetRPData() As ADODB.Recordset
On Error GoTo errHandler
'My code...bla..bla..bla...
Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."
End If
Exit Function
errHandler:
'In case there is another error that is generated by system or provider
Err.Raise Err.Number, Err.Source, Err.Description
End Function
'*******************Code in the main program
Private Sub Form_Load()
On Error GoTo errHandler
'My code and declaration......
Exit Sub
errHandler:
MsgBox Err.Number & " " & Err.Description
End Sub
-
Sep 22nd, 2000, 01:14 PM
#2
What do you mean when you say "it doesn't work"?
-
Sep 22nd, 2000, 01:32 PM
#3
Junior Member
Sanon
why do you have the line
>>Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."
when you have an error handler in the same function/sub????
as soon as u have an error, if its yours or not your code will goto the handler. If you raise the error it should still goto the handler and you should not raise another error within your handler????
Just call a msgbox.
If its your specific error then in your handler have an
if err.number=mynumber then
do something
err.clear
end if
-
Sep 22nd, 2000, 01:37 PM
#4
jfg: He has it that way because he wants the error to be detected in the calling funcyion and not in GetRPData. I think what he needs to do is this
Code:
Public Function GetRPData() As ADODB.Recordset
On Error GoTo errHandler
'My code...bla..bla..bla...
Err.Raise vbObjectError + 1000, "Mycode.Mycode", "My own error."
End If
Exit Function
errHandler:
'In case there is another error that is generated by system or provider
Err.Clear ' <-- NEW LINE
Err.Raise Err.Number, Err.Source, Err.Description
End Function
-
Sep 22nd, 2000, 01:50 PM
#5
Junior Member
yo
If that is the case he should remove the error handler from specific function and put it in the calling function.
You cannot rely on vb to pass back error's from functions it's a bit hit and miss vb seems to reset errors when it leaves functions subs after there is a handler (i.e. a goto statement).
jfg
-
Sep 22nd, 2000, 01:59 PM
#6
I'm sorry jfg but you are incorrect. It is perfectly valid to do what sanon seems to be trying to do and VB is not "hit or miss". Try this code and you will see that error 11 is reported in the calling routine.
Code:
Private Sub Form_Load()
On Error GoTo ErrorRoutine:
TheRoutineThatWillFail
Exit Sub
ErrorRoutine:
If Err.Number <> 0 Then
MsgBox "Error number " & Err.Number & " detected in calling routine"
End If
End Sub
Public Sub TheRoutineThatWillFail()
On Error GoTo ErrRtn
Err.Raise 6
Exit Sub
ErrRtn:
Err.Clear
Err.Raise 11
End Sub
-
Sep 22nd, 2000, 02:28 PM
#7
Junior Member
Sorry Martin
But I'm so use to lax programming techniques.
Besides I'm nearly sure I had this problem with vb4 and it kept reseting errors, hold on I probibly did a
on error goto handler
code here
myexit:
exit sub
handler:
if err=num then
do something
else
do something else
endif
resume myexit
end sub
''''''''''''''''''''
remember this was the recommended way to do error handlers in the olden days
jfg
-
Sep 22nd, 2000, 02:34 PM
#8
Thread Starter
Lively Member
MartinLiss, you absolutely understand my problem correctly. I'm trying to pull out the error that is generated from my GetRPData sub which is in .dll file on the server, and then my calling Form which is a client program is on the client side. Well, if you add err.clear, the user-defined error will be gone. Therefore, this solution can't solve the problem. Moreover, I can't use msgbox as well because, as I mention above, this class is running on the server side. Thus, that message box will never been seen by the client side.
Ok, let me explain what I'm understanding about err.raise method. As my example code, if this statement is executed, on the calling program, the on error goto errHandler should be worked immediately. Am I right? But in my program, when this situation happens, the calling program still ran to the next statement instead of going to error trap.
What do I miss here?
-
Sep 22nd, 2000, 03:56 PM
#9
In my example code that posted, you can assume that the TheRoutineThatWillFail rooutine is in a dll file on the server. To have it return the actual error to the client program, all you need to do is
Code:
Public Sub TheRoutineThatWillFail()
Dim lError as Long
On Error GoTo ErrRtn
Err.Raise 6
Exit Sub
ErrRtn:
lError = Err.Number
Err.Clear
Err.Raise lError
End Sub
and error number 6 (in this case) can be handled with or without a msgbox by the client program.
-
Sep 22nd, 2000, 04:35 PM
#10
Thread Starter
Lively Member
Thanks alot MartinLiss. It works just the way I want..
-
Sep 22nd, 2000, 04:56 PM
#11
You're welcome. You should also consider putting the ClassError routine below in your dll. Then call it from the error routines of each of the routines in your dll, passing the name of the routine as shown
Code:
Public Property Get IsAddNew() As Boolean
On Error GoTo ErrorRoutine
IsAddNew = m_bIsAddNew
Exit Property
ErrorRoutine:
ClassError "IsAddNew 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|