Solved

Error propagation between modules

Posted on 1998-08-18
3
352 Views
Last Modified: 2010-04-30
I have a form (a) that calls (in a different project) a class (b) that calls a form (c) that calls a class (d) ... When I raise an exception in class d it gets propogated back to form (c), but no further.  Books online hints that an error cannot be propagated further than a form.  Is this true? How can I get the error all the way back to form (a)?  I tried setting some sort of flag, but I can't seem to use the Unload command in an error handler, so I don't even know how to get out of the form.
0
Comment
Question by:gwilym
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
jsuper earned 100 total points
ID: 1429603
I have the same sort of thing going on with my classes and forms.  WHat I did was to create an Error event for the class, and when an error is trapped, I use the RaiseEvent method to return an error event to the calling class or method.  Also, I use the err.description, and the err.code as parms for the event to supply system information.  You could also just pass the error object itself as a parameter.  If you need some examples, just add a comment.
0
 

Author Comment

by:gwilym
ID: 1429604
Yup - examples sound good!
0
 
LVL 1

Expert Comment

by:jsuper
ID: 1429605
O.k.  Here is an example Imade up...   It works like a charm.  I'll show you the code, and then explain what is happening.  The project contains two forms and two classes.  The first form's code is below.  Think of it as a toybox containing buttons that when clicked, will create a toy class, and ask that a specific toy be removed from the toybox.

Private WithEvents vToy As Toys      'The toybox class defined
                                     ' with events!

Private Sub cmdBall_Click()          'Tells the toybox class to
    Set vToy = New Toys              ' get the ball out.
    vToy.Get_from_Toybox "Ball"
End Sub

Private Sub cmdTop_Click()            'Tells the toybox class to
    Set vToy = New Toys               'get the top!
    vToy.Get_from_Toybox "Top"
End Sub

                                      ' This is the only error
                                      ' message box in the
                                      ' whole project!

Private Sub vToy_Error(Code As Long, Description As String)
    MsgBox Code & " - " & Description, vbOKOnly, "Toy error!"
End Sub

Here is the code for the toybox class...  Note that it defines a variable to hold the form that is trying to return error information (frmBall)!

Public Event Error(Code As Long, Description As String)

Private WithEvents frmBall As frmBall       'Important!  This
                                            'is what allows the
                                            'form to return
                                            'events!

Public Sub Get_from_Toybox(Toy As String)
    If Toy = "Ball" Then
        Set frmBall = New frmBall            'Display the Ball
        frmBall.Show                         'Form.
    ElseIf Toy = "Top" Then
        '
    End If
End Sub

Private Sub Class_Initialize()
    Set frmBall = Nothing
End Sub

'  This event is fired when the frmBall Form raises the error
'  event!

Public Sub frmBall_Error(Code As Long, Description As String)
    RaiseEvent Error(Code, Description)
End Sub

Here is the code for the frmBall form.  It creates a class object of type Ball, and sets the appropriate properties and methods.  Notice the error event at the bottom which is fired when the ball class raises the error event...

Dim WithEvents vBall As Ball

Public Event Error(Code As Long, Description As String)

Private Sub cmdBounce_Click()
    Static force As Long
   
    force = force + 2
    vBall.Bounce force
End Sub

Private Sub Form_Load()
    Set vBall = New Ball
    vBall.Color = "Red"
    vBall.Size = 6
    txtColor = vBall.Color
    txtSize = vBall.Size
End Sub

Private Sub txtColor_Change()
    If txtColor.Text <> "" Then
        vBall.Color = txtColor.Text
    End If
End Sub

Private Sub txtSize_Change()
    If txtSize.Text <> "" Then
        vBall.Size = Val(txtSize.Text)
    End If
End Sub

Private Sub vBall_Error(Code As Long, Description As String)
    RaiseEvent Error(Code, Description)
End Sub

Last of all, here is the code for the Ball class.  Note that it traps for all kinds of errors and then uses the raiseevent function to propogate error events all the way back to the main form.  the main form then displays an error message MsgBox.  In fact, by putting error trapping in each subroutine, ALL of the classes and form errors could be routed back to one location.

Public Event Error(Code As Long, Description As String)
Private h_Color As String
Private h_Size As Long

Public Property Get Color() As String
    On Error GoTo Error
   
    Color = h_Color
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Let Color(ByVal vNewValue As String)
    On Error GoTo Error
   
    If vNewValue = "" Then
        RaiseEvent Error(1, "Color cannot be blank!")
    Else
        h_Color = vNewValue
    End If
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Get Size() As Long
    On Error GoTo Error
   
    Size = h_Size
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Property Let Size(ByVal vNewValue As Long)
    On Error GoTo Error
   
    If vNewValue < 1 Then
        RaiseEvent Error(1, "Size cannot be zeo or negative!")
    ElseIf vNewValue > 10 Then
        RaiseEvent Error(2, "Size cannot be larger than ten!")
    Else
        h_Size = vNewValue
    End If
    Exit Property
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Property

Public Sub Bounce(force As Long)
    On Error GoTo Error
   
    If force > 10 Then
        RaiseEvent Error(3, "You bounced it to hard!")
    End If
    Exit Sub
Error:
    RaiseEvent Error(Err.Number, Err.Description)
End Sub

Notice that in the error traps, I simply return the system error information back to the Main form.  For validation errors, I define my own error messages.  Good Luck, let me know if that all makes sense.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 47
VBA filters 2 37
vb6 connector to mongodb 2 26
How to incorporate an error traping routing to existing code in VB6 11 32
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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

708 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

16 Experts available now in Live!

Get 1:1 Help Now