Solved

Errors propagation

Posted on 2001-08-13
11
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Anthony, can you explain that a bit further?
0
 
LVL 6

Expert Comment

by:anthony_glenwright
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:anthony_glenwright
Comment Utility
0
 
LVL 14

Expert Comment

by:wsh2
Comment Utility
<ping>
0
 

Expert Comment

by:shlomof
Comment Utility
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
Comment Utility
a small mistake in CLASS 2:

ESC:

msgbox col("MSG")

End Sub


0
 
LVL 6

Expert Comment

by:VK
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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

10 Experts available now in Live!

Get 1:1 Help Now