Solved

Errors propagation

Posted on 2001-08-13
11
281 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

896 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now