Solved

Errors propagation

Posted on 2001-08-13
11
284 Views
Last Modified: 2012-06-21
Is there any way to propagate the error handling from a class module (Class1) to an other class module (Class2) using the first one? I would like to trap the error (on error goto...) in Class2 even if the error is generated in Class1.
In order to be clearer I'm writing you an example of the code of the two classes:

CLASS1:

Public Function OpenRs(query As String) As ADODB.Recordset

Dim Rs As New ADODB.Recordset

    Rs.Open query, conn, adOpenKeyset,  adLockBatchOptimistic

    Set openRs = Rs

End Function
'-----------------------------------

CLASS2:


Dim oClass1 As New Class1

Public Sub ManagingRs()
dim Rs as AdODB.Recorset
ON ERROR GOTO ESC:
   set RS=oClass1.OpenRs

Exit sub
ESC:
  msgbox err.description

End Sub
'--------------------------------------
If an error occurs in CLASS1, I've noticed that the execution of the program is stopped at the CLASS1 level even if the error handling is in CLASS2.

Thank you in advance


0
Comment
Question by:visualexp
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 100 total points
ID: 6378998
I am assuming that you're instantiating an object of type Class2 in a procedure in Class2, right?

In that case, simply do *not* handle the error in Class2. As a result, the processing drops back into Class1, and if the calling procedure in Class1 *does* have error handling, it will take care of it.

As an aside: to test this properly in the IDE, you must set your error handling to break on unhandled errors only.
0
 
LVL 2

Expert Comment

by:FlorisMK
ID: 6379397
This is set either through Tools-Options-General, or through right-clicking the grey margin in your code and selecting toggle...

(obviously, all points go to caraf_g!!!)
0
 
LVL 6

Expert Comment

by:anthony_glenwright
ID: 6379428
If you are operating in an MTS environment, it important to make sure the error code has the vbObjectError constant added.  COM will not propogate the error properly if you do not.

To achieve this, simply put error handling in your class module (class2) that looks like this:

IF Err.Number <> 0 THEN
  Err.Raise Err.Number Or vbObjectError, Err.Source, Err.Description
END IF
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:modder
ID: 6379440
Anthony, can you explain that a bit further?
0
 
LVL 6

Expert Comment

by:anthony_glenwright
ID: 6379475
The use of vbObjectError for COM/Automation objects is mentioned in heaps of places, but rarely with an explanation - i.e. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deconerrorhandlinginclassmodules.asp

Basically, the return value from a COM object (even in VB) is called a HRESULT.  Various bits in the HRESULT mean various things, and 'Or' ing the "real" error code with vbObjectError sets the bits that tell MTS that the result is a COM error.

To "decode" the error at the other end (the handler), use Err.Number AND NOT vbObjectError.
0
 
LVL 6

Expert Comment

by:anthony_glenwright
ID: 6379500
0
 
LVL 14

Expert Comment

by:wsh2
ID: 6379759
<ping>
0
 

Expert Comment

by:shlomof
ID: 6379860
Try this way...

CLASS1:

Public Function OpenRs(query As String, ByRef Rs As Variant) As Variant

On Error GoTo ESC:

Dim col As New Collection
Dim Rs As New ADODB.Recordset

Rs.Open query, conn, adOpenKeyset,  adLockBatchOptimistic

Set openRs = Rs

col.Add False, Key:="ERROR"
Set OpenRs= col

Exit Function

ESC:

col.Add True, Key:="ERROR"
col.Add Err.Description, Key:="MSG"
Set OpenRs = col

End Function


CLASS2:


Dim oClass1 As New Class1

Public Sub ManagingRs()

ON ERROR GOTO ESC:

dim Rs as AdODB.Recorset
Dim col As New Collection
dim strQuery as String

strQuery = " SELECT * FROM TABLENAME .....  "
set col=oClass1.OpenRs(strQuery,Rs)

if col("ERROR") then
Err.Raise
end if

Exit sub
ESC:

msgbox err.description

End Sub

0
 

Expert Comment

by:shlomof
ID: 6379867
a small mistake in CLASS 2:

ESC:

msgbox col("MSG")

End Sub


0
 
LVL 6

Expert Comment

by:VK
ID: 6380097
Hello experts ! I had a similar problem some time ago:

I wrote an OCX and used it in an EXE. When an error
occurred in the OCX (especially in an event of the OCX-class) i would be able to stop all program execution in the ocx. The decision shold be made in the EXE, because we plan to distrubute the OCX to other clients.

Because you sholudn't force a Server (the OCX) to shut down (vb cares of it, just shut down all clients so that the counter of references becomes 0).

In my case i used to copy the error object and pass it to a public error-event which could be handled in the exe.


'---------------------------------------------------------

Public Event Error(ErrorInfo As vbpCloneErr.typGtwErrInfo)

Public Function ClosePopUp() As AmbientProperties
    On Error GoTo hErr
    '_______________________________________________________________________________________________________________
    .........
    '_______________________________________________________________________________________________________________
    Exit Function
hErr:
    Select Case Err
        Case Else
            If Not ExitForever Then
                Call ErrCloner.Copy(ExtErrInfo, TMPErrInfo, Err, Erl)
                Call SetExtErrInfo(TMPErrInfo, "vbpGtwCombo", "gtwCombo", "ClosePopUp", "Function", "{ControlName}")
                Call SafeClosePopup
                RaiseEvent Error(ExtErrInfo)
                Select Case ExtErrInfo.ToDo
                    Case enmNoResponse
                        Call ShowError(ExtErrInfo)
                    Case enmResume
                        Resume
                    Case enmResumeNext
                        Resume Next
                    Case enmExitSub
                        Exit Function
                    Case enmEnd
                        ExitForever = True
                        Exit Function
                End Select
            End If
    End Select
End Function

0
 
LVL 10

Expert Comment

by:GoodJun
ID: 6381194
visualexp.,
Your code looks fine as you intend to handle the error by class2 nomatter where the error happens. Just make sure,
your error break option is set correct, and make sure the error is not handled in class1.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
passing parameter in sql procedure 9 68
Excel - Save a copy of work book 13 91
Using an encrypted  value to decrypt and display contents in vb6 9 60
Excel Automation VBA 19 104
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question