Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"On Error Resume Next" not working with "Unload" Statement

Status
Not open for further replies.

StevieK

Programmer
Jul 18, 2001
25
0
0
GB
I noticed something a bit strange in our VB5 project today... What follows is a mock up example showing the problem....

I made a little mistake by forgetting to set a Form object, and ended up getting the classic Runtime Error 91 ("Object variable or With block variable not set"), which is fair enough, so I fixed it with a Set statement... But the problem is that our app died when this error occurred, even though I'd used (what I believed to be) pretty decent error handling... It appears that the [blue]On Error Resume Next[/blue] statement will not allow execution to pass beyond an Unload statement which is given an invalid reference.

What I want to know is... How can I trap all errors? I could use a wrapper function/subroutine to do this (as shown below)... But why does [blue]On Error Resume Next[/blue] not appear to be doing what it's meant to? - It should let execution proceed no matter what happens. Shouldn't it? - That's what I'm really after.

Any suggestions are much appreciated, as I was under the impression that the way I'm doing error handling was pretty bullet-proof using [blue]Resume Next[/blue], and now it seems that I should really be using a wrapper function for every function I write.

I've not tried this out on VB6 yet. I'll try this out when I get a chance and let you all know if the same thing happens there. If you want to have a go, just use the following source in a project containing a SimpleForm.

Stephen

p.s. Obviously, I can prevent this specific problem by checking whether the Form variable [blue]Is Nothing[/blue]... But that's not the point: What I want to do is find an approach to error-handling which handles all errors.

Code:
Option Explicit
Option Base 0

Sub configureASimpleForm()

    Dim frmSimple As SimpleForm

On Error GoTo errorWhileConfiguringForm

    '' Set frmSimple = New SimpleForm   '' Uncomment this to remove the bug
    
    frmSimple.Caption = "A simple form"
    
    MsgBox "Configured a simple form successfully", vbInformation, "Sub ConfigureASimpleForm"
    
    GoTo freeResources
    
errorWhileConfiguringForm:

    Call MsgBox( _
        "Runtime Error " & Err.Number & " (" & Err.Description & ") occurred while attempting to configure a simple form.", _
        vbCritical, _
        "Sub ConfigureASimpleForm" _
    )
        
    '' Do nothing
        
freeResources:
On Error Resume Next    '' This should mean we always proceed

    ''If Not frmSimple Is Nothing Then      ''' Ucomment the If statement to prevent
                                            ''' the untrappable error from occurring
        Unload frmSimple                    '''
                                            '''
    ''End If                                '''
    
    Set frmSimple = Nothing
    
    MsgBox "Freed resources successfully", vbInformation, "Sub ConfigureASimpleForm"

    '' Do nothing

End Sub

Sub main()

On Error GoTo errorInMainSubroutine

    Call configureASimpleForm
    
    MsgBox "Successfully completed main subroutine processing", vbInformation, "Sub Main"
    
    GoTo freeResources
    
errorInMainSubroutine:

    Call MsgBox( _
        "Runtime Error " & Err.Number & " (" & Err.Description & ") occurred while running main subroutine.", _
        vbCritical, _
        "Sub Main" _
    )
        
    '' Do nothing
        
freeResources:
On Error Resume Next    '' This should mean we always proceed

    '' Do nothing
    
    MsgBox "Freed resources successfully", vbInformation, "Sub Main"

End Sub

Stephen King
Systems Developer and Support Analyst
Infoplex Ltd.
 
I tried this in VB6... [blue]On Error Resume Next[/blue] proceeds through the Unload statement with no problem. This was therefore just a bug in VB5.

Stephen King
Systems Developer and Support Analyst
Infoplex Ltd.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top