?
Solved

Errors propagation

Posted on 2001-08-13
11
Medium Priority
?
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 400 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

752 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