0

I have a few lines of code wrapped in two write log events. I know that the first part of the code is happening because I can see the first entry of the log that says the process has started but the entry that says the process completed is not being written. Somewhere in the code an error is being thrown but the error handler is not catching it for some reason. I've tried checking the event viewer for any logs about my application that Windows may have written, like it causing a crash or Windows closing it for some reason but there's nothing there. Is there a way that VB6 would skip an error handler and if so, how could I find evidence of it?

My code looks something like this:

On Error GoTo ErrorHandler

    Dim fso As FileSystemObject
    Dim DirPattern As String
    Dim FileName As String
    Dim DaysOld As Integer

    ' Init log name to ensure Today's value will be put in there
    g_PurgeLogName = ""

    Call WriteToLog("Purging of Text Files Started")

    sql = "SELECT * FROM [Text File Purge Profiles] " & _
            "WHERE User = '" & g_User & "' " & _
            "ORDER BY ProfileName;"

    Dim rs As ADODB.Recordset
    Set rs = ic.RsReadOnly(cnMF, sql)

    With rs
        Do While Not .EOF

            Set fso = New FileSystemObject

            ' create dirPattern to use with "Dir" command
            DirPattern = fso.BuildPath(!PurgePath, !FileMask)

            ' get first file matching this pattern
            FileName = Dir(DirPattern)

            Do While Len(FileName)

                FileName = fso.BuildPath(!PurgePath, FileName)

                ' how many days ago was this file created?
                DaysOld = DateDiff("d", fso.GetFile(FileName).DateCreated, Now)

                If DaysOld > !RetainDays Then
                    Debug.Print "Deleting " & FileName
                    fso.DeleteFile FileName, True
                    DoEvents
                End If

                ' get next file matching this pattern
                FileName = Dir
            Loop
            .MoveNext
        Loop
        .Close
    End With

    Call WriteToLog("Purging of Text Files Completed")

    Set fso = Nothing
    Set rs = Nothing

    Exit Sub

'=============
ErrorHandler:
'=============

    Call MsgBox2("An error occured while trying to process the Text File purge" & _
                    vbLf & vbLf & err.Number & ": " & err.Description, vbCritical)

    Call WriteToLog("Text File Purge did not complete")
    Call WriteToLog(vbTab & err.Number & ": " & err.Description)
    Call ClearScreen
    Call err.Clear

I can see that there could be issues with deleting files the program doesn't have permission to access or that there could be an issue with the query somehow but as far as I can tell, any problems that could arise should be handled by the error handler and the log would still have more text than the "process started" bit.

Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
Pendonep
  • 43
  • 6
  • 1
    does the app just halt, or does an error message appear? can you run in debug and step through? where does it fail? what is the error handling in WriteToLog? is that function failing? also try to "break on all errors" https://stackoverflow.com/questions/736434/how-to-re-enable-the-default-error-handling-in-vb6 – Jeremy Feb 25 '20 at 20:45
  • Fatal errors can't be caught. Turn off the error handler. –  Feb 25 '20 at 22:27
  • @Mark: Are you sure? Ok, maybe we talk about different 'Fatal errors'? – nabuchodonossor Feb 26 '20 at 11:04
  • If its not halting/crashing then WriteToLog() is at fault, you should add its code to your question. – Alex K. Feb 26 '20 at 11:48
  • @Mark what's an example of an error like that? – StayOnTarget Feb 26 '20 at 12:45
  • It may not apply here, but I've often had a compiled program just end, sometimes with a Windows error but often not, if an out-of-bounds array reference is made, especially on a complex type (a UDT, for example). This is true even though I don't disable array bounds checking for compilation. In the IDE, the bad reference is caught correctly. Does your program exhibit this behavior only when compiled? Can you test it in the IDE? – Mark Moulding Feb 26 '20 at 13:18
  • As Alex menttion, update your post with the code of your `WriteToLog` method. – Hel O'Ween Feb 26 '20 at 13:55
  • I don't know `MsgBox2` but if it causes an error it is inside the error handler and will exit. – itsLex Feb 26 '20 at 18:36
  • `NT Status 0xC0000005 (-1073741819) Windows Error 0x3E6 (998) NT Status Message: The instruction at 0x%p referenced memory at 0x%p. The memory could not be %s. Windows Error Message: Invalid access to memory location. ` –  Feb 26 '20 at 18:52
  • The error sounds like a standard crash message which is not able to be caught by the native VB6 error handing. You should check out this link to see how to catch memory access errors: https://stackoverflow.com/a/1101814/1640090 – vbguyny May 19 '20 at 14:44

0 Answers0