You are visitor number ????? since 11 February 2004.
Go to Howard Kaikow's home page


Copyright © 2004 by Howard Kaikow. All rights reserved.
Date: 27 February 2004
From: Howard Kaikow <kaikow@standards.com>
Subject: Out of stack space (Revision of 24 February 2004 posting)


On 31 January 2004, I posted the topic "Out of stack space: Should I punt?" in the microsoft.public.vb.general.discussion newsgroup.

The code below will demonstrate the error if you comment out the Resume statements.

The Resume statements work around the error.

Option Explicit
    Private Const strFormTitle As String = "Whatever"
    
    Private Const OUT_OF_MEMORY As Long = 7
    Private Const OUT_OF_STACK_SPACE As Long = 28
    Private Const strOut_Of_Stack_Space As String = "(OUT_OF_STACK_SPACE)"
    Private Const strOut_Of_Memory As String = "(OUT_OF_MEMORY)"

Public Sub Main()
    Dim strTemp As String
    
    Do
        strTemp = Trim$(InputBox(prompt:="Number of data items?", Title:="Sample size", Default:=""))
        On Error GoTo EndIt
        Err.Raise OUT_OF_STACK_SPACE    ' Force stack space error
        MsgBox Now, , "Cannot get here"
EndIt:
        If Err.Number <> 0 Then
            ProcessError
            Resume GoBoo
        End If
GoBoo:
        On Error GoTo EndItAgain    ' Not sufficient to clear previous error
        Err.Raise OUT_OF_STACK_SPACE    ' Force stack space error after real stack space error
        MsgBox Now, , "Nor here"
EndItAgain:
        If Err.Number <> 0 Then
            ' VB allows us to get here only if Resume was used above
            ProcessError
            Resume GoHoo
        End If
GoHoo:
        MsgBox "VB allows us to get here only if Resume is used", , "Do we get here?"
    Loop While Len(strTemp) = 0
    MsgBox "We iz done!", , "That's all folks!"
End Sub

Private Sub ProcessError()
    Dim strMessage As String
    With Err
        Select Case .Number
            Case OUT_OF_STACK_SPACE
                strMessage = strOut_Of_Stack_Space
            Case OUT_OF_MEMORY
                strMessage = strOut_Of_Memory
            Case Else
                strMessage = "Error " & .Number & ", " & .Description
                MsgBox strMessage & vbCrLf & _
                    "If possible, processing will continue.", vbOKOnly, strFormTitle
        End Select
    End With
End Sub